1.color.xml裡面定義顏色
<?xml version="1.0" encoding="utf-8"?><resources> <drawable name="darkgray">#808080FF</drawable> <drawable name="white">#FFFFFFFF</drawable><drawable name="bule">#0000FF</drawable></resources>
2.在main.xml裡面使用,通過工程運行直接顯示
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="all" android:text="百度:http://www.baidu.com" android:layout_x="10px" android:layout_y="100px" /><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="百度:http://www.baidu.com" android:textColor="@drawable/darkgray" android:background="@drawable/white" android:layout_x="10px" android:layout_y="200px" /></AbsoluteLayout>
3.如何通過程式時間來改變字型的顏色
Resources resource=this.getBaseContext().getResources();//this.getResources(); Drawable drawable=resource.getDrawable(R.drawable.white); TextView tw=(TextView) this.findViewById(R.id.text); tw.setBackgroundDrawable(drawable); /* tw.setBackgroundColor(Color.WHITE); tw.setBackgroundResource(R.drawable.white);*/
總結:1.xml裡面是配置自訂色彩的格式
<resources>
<drawable name="顏色名">#顏色值</drawable>
</resources>
2.在xml裡面引用
<TextView
....................
android:textColor="@drawable/darkgray"
android:background="@drawable/white"
.....................
/>
在根據字串的引用:
android:tex="@string/字串名"
xml定義
strings.xml
<resources>
<string name="字串名稱">字串</string>
</resources>
可以發現,這些資源的定義格式和引用的區別,只有xml檔案中元素不同而已,
在用的時候不需要去調用檔案名稱,因為這些元素直接R檔案中以ID的形式出現了
3.在程式中引用
Drawable drawable=resource.getDrawable(R.drawable.white);
tw.setBackgroundDrawable(drawable);
4.這裡關於設定TextView的背景顏色的三種方法
void setBackgroundColor(int color);
void setBackgroundDrawable(Drawable d);
void setBackgroundResource(int resid);
------------------------------------------------
tw.setBackgroundDrawable(drawable);//這裡是引用自訂的資源檔
tw.setBackgroundColor(Color.WHITE);//這裡則直接用Color類裡面的值
tw.setBackgroundResource(R.drawable.white);//這裡則以ID的形式調用