Android TextView moves down

I have an activity with two buttons and a TextView in LinearLayout. My TextView is offset down and the text does not fit inside the box. Can you explain what is going on? I think this is due to padding, and I read a few discussions about the dangers of populating a TextView, but that doesn't explain why the text is cropped at the bottom.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#800080">

    <Button
        android:text="This"
        android:background="@drawable/button_red" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <Button
        android:text="That"
        android:background="@drawable/button_green" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <TextView 
        android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons.  Why it does this I can't imagine.  I only hope someone can help me with this." 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#533f93"
    />
</LinearLayout>

This code creates this screen:

User interface

Violet - LinearLayout, blue - TextView. As you can see, the top of the TextView is lower than the buttons, and the bottom of the bottom of LinearLayout. When I add text to a TextView, LinearLayout increases its height accordingly, but since the TextView is offset, I always lose the bottom of the last line.

I launched the Hierarchy Viewer and gave me this framework:

Wireframe image from Hierarchy Viewer

, TextView. LinearLayout :

Wireframe image from Hierarchy Viewer - LinearLayout selected

Hierarchy Viewer 0, TextView 7. , , :

    android:paddingTop="0dp"
    android:background="@null"
    android:includeFontPadding="false"

.

+5
4

android:baselineAligned LinearLayout false.

:

false, . , . true.

+5

layout_gravity Textview center_vertical

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#800080">

<Button
    android:text="This"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
<Button
    android:text="That"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons.  Why it does this I can't imagine.  I only hope someone can help me with this." 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#533f93"
android:layout_gravity="center_vertical"/>
</LinearLayout>
+2

layout_gravity TextView :

<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons.  Why it does this I can't imagine.  I only hope someone can help me with this." 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#533f93"
    android:layout_gravity="top"
/>
0

If you have this problem with multiple TextViews, you can add android:gravity="center_vertical"inLinearLayout

0
source

All Articles