views:

235

answers:

1

Hi,

I'm having issues with TextViews and centering the text within the text region. Specifically, I have a TextView, but when I have text inside that contains letters that straddle down the bottom margin (i.e. p, g, q, y, etc), those letters are getting cut-off. I'm trying to center the text within the region but haven't had much luck.

[Updated] I now resolved the letters getting cut-off at the bottom using wrap_content as my height, but found another problem. It now appears that the text is positioned low in the region, leaving this gap at the top. I modified my layout to reflect the latest (see below). Basically, those characters that were getting cutoff before (g, y, j, etc) are touching the region right below which is fine, but it appears to leave a padding at the top. I tried to change the gravity to center_vertical or center, but don't have much luck:

Note, I have to work with the specs given the textSizes (i.e. I can't change the values for these)

<LinearLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="43.3dip"
    android:background="@drawable/custom_bg"
    android:orientation="horizontal">
    <ImageButton
        android:id="@+id/headshot"
        android:layout_width="43.3dip"
        android:layout_height="43.3dip"
        android:src="@drawable/sample"
        android:background="@drawable/head_btn"
        android:layout_gravity="center" />
    <RelativeLayout
        android:id="@+id/name_and_email"
        android:layout_width="230.7dip"
        android:layout_height="43.3dip"
        android:orientation="vertical">


    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/custom_color"
        android:textSize="18.6sp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:background="#c5ff15"
        android:lineSpacingExtra="0sp"
        android:text="AaBbCcDdGgJjTtYy" />
    <TextView
        android:id="@+id/email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/custom_color"
        android:textSize="13.3sp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_centerInParent="true"
        android:layout_below="@id/name"
        android:gravity="center_vertical"
        android:visibility="gone"
        android:text="[email protected]" />


    </RelativeLayout>
    <ImageButton
        android:id="@+id/headshot2"
        android:layout_width="43.3dip"
        android:layout_height="43.3dip"
        android:src="@drawable/sample"
        android:background="@drawable/head_btn2"
        android:layout_gravity="center" />
</LinearLayout>

Can anyone help?

MB

+1  A: 

Why are you trying to set the heights manually? You will have much better luck using layout_height="wrap_content". I'd imagine that the problem is that your text size of 14.3sp is too big for the area you are allowing it.

Also, a good resource for debugging complex layouts is the HeirarchyViewer, found under the tools folder.

Mayra
Thanks for your response. I actually now moved it to wrap_content and that did solve the text descender problem that I have. But now I have another problem: the text itself looks like its shoved down to the bottom leaving this "gap" or padding at the top of my region. I tried to center it using center_vertical but it does not work. I modified the layout of the two TextViews above such that the height is now wrap_content and the android:gravity to "center_vertical" but I still have some issues (the g's and p's now fit, but it appears it has a gap at the top)
mbethdev
You should be able to edit your original question with your updated xml. Its hard to tell what exactly you have now, but I'd try using the heirarchy viewer to see who exactly is responsible for the extra space. Then you should be able to adjust padding, etc. on that element.
Mayra
Ok, I modified they layout to simplify it a bit (so if you did get a chance to load the layout on Eclipse, you should only see the top textview). As far as I know the padding is happening *within* the text region and its something that I should be able to control inside TextView but the gravity doesn't seem to work. I must be missing another attribute but don't know what it is :(
mbethdev
Did you try padding, or paddingTop, or includeFontPadding? See http://developer.android.com/reference/android/widget/TextView.html for a full list of choices.
Mayra
hmm...interesting I could've sworn when I looked at the site these attributes weren't here before but you're right, there's a lot I can play around with here. Thanks for the info!
mbethdev