Using color in Android, by XML
2011-11-29 10:08
477 查看
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 (withany file name), with your own color id defined inside. Such that you can access them in Java code using id. Android also defined a base set
inandroid.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>
相关文章推荐
- How to change the Text color of Menu item in Android?
- Using your own SQLite database in Android applications
- Using Google Maps in Android
- Using pthread in Android
- Using MultipartEntity in Android applications
- Using AlarmManager and BroadcastReceiver in Android
- Using Android in Industrial Automation
- Using Styles and Themes in Android(在Android中使用主题和样式)
- android向服务器上传multipart/form-data文件(upload using multipart post using httpclient in android)
- 在Android程序里显示等待滚动圈(使用ProgressDialog)| Show waiting rolling circle in android application (using ProgressDialog)
- 【转载】Using SQLite from Shell in Android(在shell 下使用sqlite命令操作数据库)
- Android: How to download the latest zip Android Source Code easily and using it in Intellij
- Using Flot's Bar Graph in an Android WebView with Highlighting
- using global variable in android extends application
- Generate mp4 video with image files using MediaCodec in Android
- Using your own SQLite database in Android applications
- Asynchronous HTTP Requests in Android Using Volley
- Warning:Using incompatible plugins for the annotation processing: android-apt. This may result in an
- Using Android monkeyrunner from Eclipse, both in Windows and Linux!
- what is the purpose of using translatable in Android strings?