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