安卓TextView向下偏移

5

我有一个包含两个按钮和一个文本视图的线性布局。我的文本视图向下偏移,文本无法适应框内。你能解释一下这是怎么回事吗?我认为这与填充有关,我已经阅读了几篇关于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>

这段代码生成了这个显示效果: 用户界面 紫色是LinearLayout,蓝色是TextView。如您所见,TextView的顶部在按钮之下,底部在LinearLayout之下。随着我向TextView添加文本,LinearLayout会适当增加其高度,但由于TextView被偏移,我总是丢失最后一行的底部。
我运行了Hierarchy Viewer并得到了这个线框图: Hierarchy Viewer中的线框图像 它显示了顶部的垂直偏移量,但错过了TextView的底部。选择LinearLayout的同一线框图如下所示: 从Hierarchy Viewer中的线框图像 - 选择LinearLayout 根据Hierarchy Viewer,按钮的顶部为0,但TextView的顶部为7。我尝试了各种修复方法,大多数来自于此站点:
    android:paddingTop="0dp"
    android:background="@null"
    android:includeFontPadding="false"

这些方法都没有解决我的问题。

4个回答

6

将您的LinearLayoutandroid:baselineAligned属性设置为false

来自文档:

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


是的,那个可行!我不明白为什么默认情况下LinearLayout会将文本推出其边界。 - TomDestry
1
你可以阅读例如这篇文章来理解 baseline 的意思。因此,默认情况下,LinearLayout 中的每个后续小部件都与其前面的小部件基线对齐。在我们的例子中,TextView第一行Button 的文本基线对齐。我同意,这不是直观的行为,但它就是这样。 - StenaviN

2
将Textview的layout_gravity属性设置为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>

哎呀!太简单了,谢谢!默认似乎是“无”。你有什么想法为什么默认情况下它会将文本部分推出视线? - TomDestry

0
如果您在多个TextView上遇到此问题,可以将android:gravity="center_vertical"添加到LinearLayout

0
尝试像这样为TextView设置layout_gravity:
<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"
/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接