2012-05-16 126 views
5

我在LinearLayout中有两个按钮和一个TextView。我的TextView向下偏移,文本不适合框内。你能解释发生了什么吗?我认为它与填充相关,并且我已经阅读了关于TextView填充风险的几个讨论,但是这并不能解释为什么文本在底部被切掉。Android 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偏移,我总是会丢失最后一行的底部。

我跑层次浏览器,它给了我这个线框:

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" 

这些都不是我的问题。

回答

5

设置android:baselineAligned您的LinearLayout的财产至false

从技术文档:

当设置为,防止从布局调整其儿童 基线。当儿童使用不同的重力值时,此属性特别有用。默认值是true

+0

是的,那有效!我不明白为什么默认值是LinearLayout将文本推送到边界之外。 – TomDestry

+1

例如,您可以阅读这篇文章,了解[基准意味着什么](https://groups.google.com/d/topic/android-developers/1gPy9zo28ak/discussion)。因此,默认情况下'LinearLayout'中的每个后续小部件都与其前一个小部件基线对齐。在我们的例子中,** TextView的第一行**与[Button]的文本基线对齐。我同意,这不是直观的行为,但它是。 – StenaviN

0

尝试将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

它不会工作 – 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> 
+0

Gah!很简单,谢谢!默认情况下似乎是NONE。任何想法为什么默认它推动文本部分在视线之外? – TomDestry

0

如果你有这个问题与几个TextViews您可以添加android:gravity="center_vertical"LinearLayout