2016-03-04 43 views
1

我有我正在處理的這個項目,並且我有一個包含兩個TextView的LinearLayout。我已經使用android:background =「@color/...」設置了LinearLayout的背景顏色,但此適用僅適用於textviews結束的位置。如果它在屏幕中間結束,則顏色停在那裏,其餘的顏色顯示爲白色。 我不明白。我徹底搜索無濟於事。 請大家幫幫我。這是我的佈局文件。如何更改android中包含文本視圖的整個線性佈局的背景顏色

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/gold"> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/gold" 
    android:paddingBottom="10dp" 
    android:paddingLeft="15dp" 
    android:paddingTop="10dp" 
    android:textSize="20sp" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/gold" 
    android:paddingLeft="15dp" 
    android:paddingRight="10dp" 
    android:textSize="15sp" /> 

</LinearLayout> 

問候。

回答

3

您的LinearLayout可能只使用wrap_content。如果你想在全屏幕是彩色的,你的LinearLayout必須延伸到使用全屏幕:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/my_color> 

</LinearLayout> 

如果你的LinearLayout是不是在你的佈局根元素,然後將背景設置爲根元素,確保它對寬度和高度都有match_parent。

+1

我的線性佈局是根元素,我已經設置了佈局的高度和寬度以匹配父級,但它仍然沒有工作。 –

+0

如果您的LinearLayout用作ListView的單元佈局?如果是這樣,請在Listview中設置背景,而不是單元格本身。 – Francesc

+0

非常感謝。剛剛解決它。 上帝保佑。 –

0

我認爲......主LinearLayout的寬度設置爲wrap_content。 將其更改爲match_parent。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/your_color" 
    android:orientation="vertical"> 

    .... 
</LinearLayout> 
+0

我試過了,但似乎沒有幫助。我只是在原始文章中添加了佈局文件。感謝您的日常支持。 –

+0

將textview的高度更改爲wrap_content –

+0

嘗試過但同樣的事情。值得一提的是,這些textview使用來自數據庫和數據的數據填充。該列表使用和自定義列表適配器顯示。這部分工作。我只需要將顏色從白色改爲金色。 感謝您的日常支持。 –

相關問題