1. 程式人生 > >Android UI詳解之顏色資源的使用

Android UI詳解之顏色資源的使用

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

                Android 開發中使用的顏色可以分為兩種,自定義顏色和系統顏色

1.自定義顏色:

顏色值的定義是通過RGB三原色和一個alpha值來定義的(加色原理)。以井號(#)開始,後面是Alpha-Red-Green-Blue的格式。
形如:
#RGB
#ARGB
#RRGGBB
#AARRGGBB

通常使用#RRGGBB 或者#AARRGGBB的形式

1.1 在資原始檔中定義顏色:
一般在res\values下建立colors.xml檔案,定義顏色,如下:

<?
xml version="1.0" encoding="utf-8"?>
<resourses> <color name="red">#ff0000</color> </resourses>


1.2 顏色的使用
1.2.1 在程式碼中使用顏色
R.color.color_name
例如:

Button btn1 = (Button) findViewById(R.id.button1); int color = Resources.getSystem().getColor(R.color.red); btn1.setBackgroundColor(color); 

1.2.2 在佈局檔案中使用顏色
@[package:]color/color_name
例如:

<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Address book" android:background="@color/red" ></Button> 


這個地方也可以直接使用顏色值,但是不推薦這樣做
<Button android:id
="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Address book" android:background="#ff0000" >
</Button>


2.系統顏色
android也有一些內建的顏色,例如系統資源中定義的顏色,十分有限。
android.graphics.Color類中也提供了一些顏色常量和構造顏色值的靜態方法。

2.1 系統顏色的使用
2.1.1 在程式碼中使用系統顏色

系統資源中定義的顏色值十分有限
Button btn1 = (Button) findViewById(R.id.button1);//系統資源一定通過int型別資源去訪問int color = Resources.getSystem().getColor(android.R.color.background_dark); btn1.setBackgroundColor(color); 


Color類中的顏色常量
Button btn1 = (Button) findViewById(R.id.button1); btn1.setBackgroundColor(Color.CYAN); 


使用Color類中的靜態方法
Button btn1 = (Button) findViewById(R.id.button1); btn1.setBackgroundColor(Color.argb(0xff, 0xff, 0x00, 0x00)); 

2.1.2 在佈局檔案中使用系統顏色
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Address book" android:background="@android:color/background_dark" ></Button>


           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述