Android定义color资源

来源:互联网 发布:c语言好不好学 编辑:程序博客网 时间:2024/06/05 07:29

在Android中可以自定义color资源,就像使用string资源一样,在/res/values/下面建立xml文件

格式如下:

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

<resources> 
    <color name="red">#f00</color> 
    <color name="blue">#0000ff</color> 
    <color name="green">#f0f0</color> 
    <color name="main_back_ground_color">#ffffff00</color> 
</resources>

 

Color Resources in Java code  
int mainBackGroundColor  =  activity.getResources.getColor(R.color.main_back_ground_color);

Using Colors in View Definitions 
<TextView android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:textColor="@color/ red" 
          android:text="Sample Text to Show Red Color"/>