3

I am using the following code and when I set gravity to the first textview to center, automatically the second textview's text is also getting aligned with the first one. Even Though I set the gravity of second view to top

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="96dp"
        android:text="New Text"
        android:id="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="New Text"
        android:id="@+id/textView2" />
</LinearLayout>

There was a solution in another question which says to wrap the 2nd textview in another LinearLayout. But why is it so?

4

1 回答 1

2

默认情况下,水平LinearLayout将其子Views 与它们的基线对齐,因此第二个TextView被移动以使其文本与第一个对齐。要解决您的问题,只需将LinearLayout'baselineAligned属性设置为false.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">
于 2016-06-25T06:03:44.220 回答