2011-09-19 61 views

回答

46

取出外部佈局(例如LinearLayout)並將其背景屬性設置爲某種顏色。

<LinearLayout android:background="@color/mycolor" 
       .... /> 

這些顏色可以在res/values/colors.xml文件定義(見here如何做到這一點)

您也可以直接在屬性(android:background="#ffff0000")上定義顏色,但通常不好。通過定義XML文件中的顏色,您可以給它一個描述性名稱(提高代碼可讀性),您可以在其他地方重新使用它。


編輯:
即使世界在我鏈接的文檔的例子,但這裏是一個簡短的例子它的外觀:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="red">#ffff0000</color> 
    <color name="green">#ff00ff00</color> 
</resources> 

它基本上是一個resources標籤包含多個color標籤。每種顏色都有一個name屬性(用於引用顏色)和實際顏色。這是在十六進制顏色標籤之間定義的。請參閱文檔以瞭解可能的格式。這一個是#AARRGGBB,其中A=alpha(透明度),R=red,G=greenB=blue。此示例文件包含全紅和全綠顏色。它們可以通過@color/red@color/green來引用。

+0

你能告訴我如何設置colors.xml文件嗎?我創建了一個新的XML文件,但我如何設置顏色值? – gsfd

+0

編輯了一個簡短的解釋。 :) – 2011-09-19 22:36:31

2

有一點需要提及的是,如果將此句子應用於include指令,"android:background="#ffffffff""設置不起作用。

例如,

<include 
    android:id="@+id/fragment_printer_detail_property_group" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="@dimen/printer_detail_group_vertical_margin" 
    android:layout_marginLeft="@dimen/printer_detail_group_horizontal_margin" 
    android:layout_marginRight="@dimen/printer_detail_group_horizontal_margin" 
    android:layout_marginTop="@dimen/printer_detail_group_vertical_margin" 
    layout="@layout/module_printer_detail_property" 
    android:background="@color/module_printer_detail_group_background_color" /> 

"android:background"應module_printer_detail_property的佈局文件中設置。