4

In the screenshot below, I am attempting to get the text in the blue box to be aligned in the centre. It is to low down and needs to be lifted a pixel or two. I have been playing with the padding but it does not seem to have any effect.

enter image description here

The layout XML is here :

<RelativeLayout  xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginRight="5dp"
xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView android:id="@+id/tvName" 
        android:layout_width="60dp" 
        android:layout_height="20dp" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="3dp" 
        android:background="@drawable/rectanglesegment" 
        android:gravity="center"
        android:paddingBottom="2dp" 
        android:text="XXX" android:textSize="16sp" 
        android:textAppearance="?android:attr/textAppearanceMedium"/>

Any suggestions?

After feedback from @Snicolas I now have the following :

enter image description here

With this layout

<TextView android:id="@+id/tvName" 
android:layout_width="60dp" 
android:layout_height="fill_parent" 
android:layout_marginLeft="10dp" 
android:layout_marginRight="10dp" 
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="@drawable/rectanglesegment" 
android:gravity="center"
android:text="abv" android:textSize="16sp" 
android:textAppearance="?android:attr/textAppearanceMedium"/>
BENBUN Coder
  • 4,801
  • 7
  • 52
  • 89
  • paddingTop -2 didn't work ? – Snicolas Dec 30 '12 at 15:47
  • you could also try with android:capitalize = true. – Snicolas Dec 30 '12 at 15:49
  • 1
    This thread is for you : http://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertical-in-a-textview-in-android – Snicolas Dec 30 '12 at 15:52
  • @Snicolas It is already using `android:gravity="center"`... – quietmint Dec 30 '12 at 15:54
  • What did you try in the edit, it's not clear ? And did you see I suggested a negative top padding ? – Snicolas Dec 30 '12 at 16:07
  • @Snicola The paddingTop -2 (with the negative) did not seem to have any effect at all. The link to the stackoverflow.com/questions/432037/… pointed me in the right direction by having android:layout_height="fill_parent" and then that seemed to let the existing android:gravity="center" work. – BENBUN Coder Dec 30 '12 at 16:10
  • So you can accept your own answer and accept it. That's fine and will be more usefull for other people with same question. – Snicolas Dec 30 '12 at 16:36

3 Answers3

2

It looks like the extra room on top is just font padding(for accents, etc). Try this:

<TextView
    ...
    android:includeFontPadding="false"
/>
Geobits
  • 22,218
  • 6
  • 59
  • 103
0

How about playing with android:lineSpacingExtra or android:lineSpacingMultiplier? These XML attributes call setLineSpacing().

public void setLineSpacing (float add, float mult)

Sets line spacing for this TextView. Each line will have its height multiplied by mult and have add added to it.

quietmint
  • 13,885
  • 6
  • 48
  • 73
0

You should look at the background image - rectanglesegment. It is probably 9 patch image and needs adjustment.

Vasudev
  • 1,936
  • 19
  • 17