2012-05-16 128 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