【问题标题】:Android TextView offset downwardsAndroid TextView 向下偏移
【发布时间】:2012-05-16 03:32:10
【问题描述】:

我在 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>

这段代码会产生这样的显示:

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

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

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

根据 Hierarchy Viewer,按钮的顶部为 0,但 TextView 的顶部为 7。我尝试了各种修复,大部分来自此站点:

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

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

【问题讨论】:

    标签: android android-layout textview gravity


    【解决方案1】:

    LinearLayoutandroid:baselineAligned 属性设置为false

    来自文档:

    当设置为 false 时,阻止布局对齐其子元素的 基线。这个属性在孩子使用时特别有用 不同的重力值。默认值为 true

    【讨论】:

    • 是的,这行得通!我不明白为什么默认是 LinearLayout 将文本推到其边界之外。
    • 您可以阅读例如这篇文章来了解baseline mean 的含义。因此,默认情况下,LinearLayout 中的每个后续小部件都与其前一个小部件基线对齐。在我们的例子中,TextViewfirst 行与Button 的文本基线对齐。我同意,这不是直觉行为,但确实如此。
    【解决方案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>
    

    【讨论】:

    • 啊!这么简单,谢谢!默认值似乎是 NONE。知道为什么默认情况下它会将文本部分推到看不见的地方吗?
    【解决方案3】:

    尝试像这样为 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"
    />
    

    【讨论】:

      【解决方案4】:

      如果您对多个 TextView 有此问题,您可以将 android:gravity="center_vertical" 添加到 LinearLayout

      【讨论】:

        猜你喜欢
        • 2021-04-27
        • 1970-01-01
        • 2021-04-13
        • 1970-01-01
        • 1970-01-01
        • 2013-11-10
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多