Search code examples
androidtextviewandroid-linearlayout

What causes TextView to vertical shift


What causes Text1 to shift vertically? The shift also depend on Text2 font size, and not depends on container, or text2 height.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="68sp"
        android:text="text2"/>
</LinearLayout>

Solution

  • By default, LinearLayout tries to align TextViews baselines (the bottom edge of the text).

    To disable this behavior, add

    android:baselineAligned="false"
    

    to your LinearLayout