Using color in Android, by XML

In Android, colors can be defined directly, such as "#ff0000" for red. You can also create a resource file under /res/values folder(with any file name), with your own color id defined inside. Such that you can access them in Java code using id. Android also defined a baseset in android.R.color.

example:



/res/values/mycolor.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> <color name="green">#00ff00</color> <color name="blue">#0000ff</color> </resources> 


/res/layout/main.xml
<?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:id="@+id/background" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <!-- "white" defined in Android base set of colors --> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="WHITE" android:textColor="@android:color/white" android:id="@+id/whitebutton" /> <!-- direct define textColor --> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="RED" android:textColor="#ff0000" android:id="@+id/redbutton" /> <!-- "green" defined in mycolor.xml --> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="GREEN" android:textColor="@color/green" android:id="@+id/greenbutton" /> <!-- "blue" defined in mycolor.xml --> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="BLUE" android:textColor="@color/blue" android:id="@+id/bluebutton" /> </LinearLayout>

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)4.x下基于wm8994的mic检测
  2. Android四大组件中每个组件的作用是什么?
  3. 认识 android 中的 LayoutInflater
  4. Android教程之如何使用自定义字体
  5. 不同android 版本的webview底层实现有差
  6. Android下载apk文件并安装
  7. Android(安卓)设计模式 之 观察者模式
  8. Android学习笔记_4_单元测试
  9. Android(安卓)recovery 下恢复备份文件
  10. AndroidManifest.xml文件详解(instrumenta