Android TextView向下偏移

Tom*_*try 5 android gravity textview android-layout

我有一个活动,在LinearLayout中有两个按钮和一个TextView.我的TextView向下偏移,文本不适合框内.你能解释一下发生了什么吗?我认为它与填充有关,我已经阅读了几篇关于TextView填充的危险的讨论,但这并不能解释为什么文本在底部被截断.

<?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>
Run Code Online (Sandbox Code Playgroud)

此代码生成此显示:

用户界面

紫色是LinearLayout,蓝色是TextView.如您所见,TextView的顶部低于按钮的顶部,其底部低于LinearLayout的底部.当我向TextView添加文本时,LinearLayout会适当地增加其高度,但由于TextView是偏移的,所以我总是丢失最后一行的底部.

我运行了Hierarchy Viewer,它给了我这个线框:

来自层次结构查看器的线框图像

它显示了顶部的垂直偏移,但是错过了TextView的底部.选择了LinearLayout的相同线框如下所示:

来自层次结构查看器的线框图像 - 已选择LinearLayout

根据Hierarchy Viewer,按钮的顶部是0,但TextView的顶部是7.我尝试了各种修复程序,大多是从这个网站中挑选出来的:

    android:paddingTop="0dp"
    android:background="@null"
    android:includeFontPadding="false"
Run Code Online (Sandbox Code Playgroud)

这些都没有解决我的问题.

Ste*_*viN 5

设置android:baselineAligned你的财产LinearLayoutfalse.

来自文档:

设置为false时,阻止布局对齐其子项的基线.当孩子使用不同的重力值时,此属性特别有用.默认值为true.