我在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>
此代码生成该显示:
的紫色是的LinearLayout,蓝色是TextView的。正如你所看到的,TextView的顶部低于按钮的底部,底部低于LinearLayout的底部。当我将文本添加到TextView中时,LinearLayout会适当地增加其高度,但由于TextView偏移,我总是会丢失最后一行的底部。
我跑层次浏览器,它给了我这个线框:
足见其垂直上方偏移,但是偏出TextView的底部。同样的线框与LinearLayout中选择看起来像这样:
根据层级体系,浏览器,按钮的上方为0,但是TextView的顶部是在7,我已经试过各种修复,主要是从这个网站挑选出来的:
android:paddingTop="0dp"
android:background="@null"
android:includeFontPadding="false"
这些都不是我的问题。
是的,那有效!我不明白为什么默认值是LinearLayout将文本推送到边界之外。 – TomDestry
例如,您可以阅读这篇文章,了解[基准意味着什么](https://groups.google.com/d/topic/android-developers/1gPy9zo28ak/discussion)。因此,默认情况下'LinearLayout'中的每个后续小部件都与其前一个小部件基线对齐。在我们的例子中,** TextView的第一行**与[Button]的文本基线对齐。我同意,这不是直观的行为,但它是。 – StenaviN