本文转自:http://blog.csdn.net/eclipsexys/article/details/8790626
- <?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>