在Android中,如果需要改变控件默认的颜色,包括值的颜色,需要预先在strings.xml中设置,类似字符串,可以反复调用。Android中颜色可以使用drawable或是color来定义。
本例中strings.xml内容:
1 2 3 4 5 6 7 8 9 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Main!</string> <string name="app_name">Color</string> <drawable name="red">#ff0000</drawable> <color name="gray">#999999</color> <color name="blue">#0000ff</color> <color name="background">#ffffff</color> </resources> |
上面定义了几个颜色值,下面是在布局文件中的调用,main.xml内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background" > <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="@drawable/red" /> <TextView android:id="@+id/tv2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="@color/gray" /> <TextView android:id="@+id/tv3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> |
在Java程序中使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
这里以TextView为例,其他控件类似.
© 2011, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记