Home > Software engineering >  LinearLayout adds unwanted top margin to Textview when text is wrapped
LinearLayout adds unwanted top margin to Textview when text is wrapped

Time:09-02

I have a LinearLayout that shows a few Textview objects next to eachother. The middle two have width set to 0dp and both a weight of 1 and a fixed height of 22sdp

Whenever the Textview has text that needs to be wrapped, I get an unwatned margin which causes an ugly alignment with the other fields.

I tried changing the gravity of the Textview, setting Margin to 0dp manually but no result so far. (when I increase the weight until the textview has enough space to show the text in a single line everything is fine again, offcourse this is not a solution that I want)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightTransparent"
>

<TextView
    android:id="@ id/txtSlotName"
    android:layout_width="@dimen/_80sdp"
    android:layout_height="@dimen/_22sdp"
    android:gravity="center_vertical"
    android:paddingStart="@dimen/_4sdp"
    android:text="Slot #1"
    android:textColor="@color/softWhite"
    android:textSize="@dimen/font_medium"
   />
<TextView
    android:id="@ id/txtManagerName"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="@dimen/_22sdp"
    android:gravity="center_vertical"
    android:paddingStart="@dimen/_4sdp"
    android:text="Manager Name"
    android:textColor="@color/softWhite"
    android:textSize="@dimen/font_medium"
    />
<TextView
    android:id="@ id/txtTeamName"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="@dimen/_22sdp"
    android:gravity="center_vertical"
    android:paddingStart="@dimen/_4sdp"
    android:text="Team Name Team Name Team Name Team Name"
    android:textColor="@color/softWhite"
    android:textSize="@dimen/font_medium"
    />
<TextView
    android:id="@ id/txtGameDate"
    android:layout_width="@dimen/_60sdp"
    android:layout_height="@dimen/_22sdp"
    android:gravity="center_vertical"
    android:paddingStart="@dimen/_4sdp"
    android:text="15 DEC 2021"
    android:textColor="@color/softWhite"
    android:textSize="@dimen/font_medium"
    />

The Textview gets a margin when it's wrapped

CodePudding user response:

Issue is solved, the comment of Mike M. on the OP did the trick:

Add android:baselineAligned="false" to the <LinearLayout>. By default, it's lining up the first lines of text in your TextView

  • Related