## 顏色
* 在Android中,顏色值由透明度alpha和RGB(紅、綠、藍)三原色定義,有八位十六進制數與六位十六進制數兩種編碼。
* 例如八位編碼FFEEDDCC,FF表示透明度,EE表示紅色的濃度,DD表示綠色的濃度,CC表示藍色的濃度。透明度為FF表示完全不透明,為00表示完全透明。
* 六位十六進制編碼有兩種情況,在XML文件中默認不透明(透明度為FF),在代碼中默認透明(透明度為00)。
所以在代碼中要使用八位的十六進制數來表示顏色,因為六位的十六進制數在代碼中是透明的。
下面的代碼分別給兩個文本控件設置六位編碼和八位編碼的背景色。
```
// 從布局文件中獲取名叫tv_code_six的文本視圖
TextView tv_code_six = findViewById(R.id.tv_code_six);
// 給文本視圖tv_code_six設置背景為透明的綠色,透明就是看不到
tv_code_six.setBackgroundColor(0x00ff00);
// 從布局文件中獲取名叫tv_code_eight的文本視圖
TextView tv_code_eight = findViewById(R.id.tv_code_eight);
// 給文本視圖tv_code_eight設置背景為不透明的綠色,即正常的綠色
tv_code_eight.setBackgroundColor(0xff00ff00);
```
XML文本布局的代碼如下圖
~~~
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XML設置六位背景顏色"
android:textSize="18sp"
android:background="#00ff00" />
<TextView
android:id="@+id/tv_code_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代碼設置六位背景顏色"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_code_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="代碼設置八位背景顏色"
android:textSize="18sp" />
</LinearLayout>
~~~
如圖所示
:-: 
圖1 不同方式設置顏色編碼的效果圖
在Android中使用顏色有下列3種方式:
1. 使用系統已定義的顏色常量。
Android系統有12種已經定義好的顏色,具體的類型定義在Color類中,詳細的取值說明見表2-1。

2. 使用十六進制的顏色編碼。
在布局文件中設置顏色需要在色值前面加“#”,如`android:textColor="#000000"`。在代碼中設置顏色可以直接填八位的十六進制數值(如`setTextColor(0xff00ff00);`),也可以通過`Color.rgb(int red, int green, int blue)`和`Color.argb(int alpha, int red, int green, int blue)`這兩種方法指定顏色。在代碼中一般不要用六位編碼,因為六位編碼在代碼中默認透明,所以代碼用六位編碼跟不用沒什么區別。
3. 使用colors.xml中定義的顏色。
res/values目錄下有個colors.xml文件,是顏色常量的定義文件。如果要在布局文件中使用XML顏色常量,可引用“@color/常量名”;如果要在代碼中使用XML顏色常量,可通過這行代碼獲取:`getResources().getColor(R.color.常量名)`。
在 XML 中定義的顏色值。顏色使用 RGB 值和 alpha 通道指定。您可以在接受十六進制顏色值的任何地方使用顏色資源。當 XML 中需要可繪制資源時,您也可以使用顏色資源(例如,`android:drawable="@color/green"`)。
該值始終以井號 (#) 字符開頭,后跟以下某種格式的“透明度、紅、綠、藍”(Alpha-Red-Green-Blue) 信息:
* #RGB
* #ARGB
* #RRGGBB
* #AARRGGBB
**注意**:顏色是使用`name`屬性中提供的值(而不是 XML 文件的名稱)引用的簡單資源。因此,您可以在一個 XML 文件中將顏色資源與其他簡單資源合并到一個`<resources>`元素下。
**文件位置**:
`res/values/colors.xml`
該文件名可以任意設置。`<color>`元素的`name`將用作資源 ID。
**資源引用**:
在 Java 中:`R.color.*color_name*`
在 XML 中:`@[*package*:]color/*color_name*`
**語法**:
```
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color
name="color_name"
>hex_color</color>
</resources>
```
**元素**:
* `<resources>`
**必需**。該元素必須是根節點。
沒有屬性。
* `<color>`
以十六進制表示的顏色,如上所述。
屬性:
* `name`
字符串。顏色的名稱。該名稱將用作資源 ID。
**示例**:
保存在`res/values/colors.xml`的 XML 文件:
~~~
? ? <?xml version="1.0" encoding="utf-8"?>
? ? <resources>
? ? ? ?<color name="opaque_red">#f00</color>
? ? ? ?<color name="translucent_red">#80ff0000</color>
? ? </resources>
~~~
以下應用代碼會檢索顏色資源:
~~~
? ? Resources res = getResources();
? ? int color = res.getColor(R.color.opaque_red);
~~~
以下布局 XML 會將該顏色應用到屬性:
~~~
? ? <TextView
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:textColor="@color/translucent_red"
? ? ? ? android:text="Hello"/>
~~~