1

I have 2 TextViews inside a linear layout with vertical orientation for controls, i want the text in the top TextView be aligned to bottom and the text in the bottom TextView be aligned to the top, however no matter what I do with altering the gravity of the TextViews the text is vertically centered in both of them. I need the text to be almost touching with small gap

Here is the xml

    <LinearLayout
    android:clickable="true"
    android:background="@drawable/dialler_button"
    android:layout_marginTop="2dip"
    android:layout_marginLeft="2dip"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical">

        <TextView  
            android:id="@+id/number2"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:text="2"  
            android:gravity="center_horizontal|bottom"
            android:layout_weight="1"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:textSize="30sp"/>  

        <TextView  
            android:id="@+id/number2letters"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:text="ABC"  
            android:gravity="center_horizontal|top"
            android:layout_weight="2"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:textSize="13sp"/>  


     </LinearLayout>
tech74
  • 301
  • 5
  • 20
  • I am trying to create a button like you see on the android dialler app eg the number 2 with ABC underneath, the ABC text is slightly smaller but very close to the bottom of the number 2, hope its clear – tech74 Jan 22 '12 at 21:58

2 Answers2

3

The attributes gravity and layout_gravity are commonly confused. In this case, from your TextViews you want to set the gravity of the TextView within its parent, so you should use layout_gravity instead of gravity. gravity sets gravity for views that are children of the view you set the attribute on.

Community
  • 1
  • 1
kabuko
  • 36,028
  • 10
  • 80
  • 93
1

Try this:

<LinearLayout
android:clickable="true"
android:background="@drawable/dialler_button"
android:layout_marginTop="2dip"
android:layout_marginLeft="2dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">

    <TextView  
        android:id="@+id/number2"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="2"  
        android:gravity="center_horizontal|bottom"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:textSize="30sp"/>  

    <TextView  
        android:id="@+id/number2letters"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="ABC"  
        android:gravity="center_horizontal|top"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:textSize="13sp"/>  


 </LinearLayout>
Matt
  • 156
  • 4