Android TextView向下偏移

时间:2012-05-16 03:32:10

标签: android android-layout textview gravity

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

此代码生成此显示:

User interface

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

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

Wireframe image from Hierarchy Viewer

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

Wireframe image from Hierarchy Viewer - LinearLayout selected

根据层次结构查看器,按钮顶部为0,但TextView的顶部为7.我尝试了各种修复程序,大多是从这个网站中挑选出来的:

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

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

4 个答案:

答案 0 :(得分:5)

android:baselineAligned的{​​{1}}属性设为LinearLayout

来自文档:

  

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

答案 1 :(得分: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>

答案 2 :(得分: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"
/>

答案 3 :(得分:0)

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