1. Define the color in color. xml
<?xml version="1.0" encoding="utf-8"?><resources> <drawable name="darkgray">#808080FF</drawable> <drawable name="white">#FFFFFFFF</drawable><drawable name="bule">#0000FF</drawable></resources>
2. Use it in Main. xml and directly display it through project running
<Absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: autolink = "all" Android: text = "http://www.baidu.com:" Android: layout_x = "10px" Android: layout_y = "100px"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "http://www.baidu.com:" Android: textcolor = "@ drawable/darkgray" Android: Background = "@ drawable/White" Android: layout_x = "10px" Android: layout_y = "200px"/> </absolutelayout>
3. How to change the font color through program time
Resources resource=this.getBaseContext().getResources();//this.getResources(); Drawable drawable=resource.getDrawable(R.drawable.white); TextView tw=(TextView) this.findViewById(R.id.text); tw.setBackgroundDrawable(drawable); /* tw.setBackgroundColor(Color.WHITE); tw.setBackgroundResource(R.drawable.white);*/
Conclusion: 1. the XML format is used to configure custom colors.
<Resources>
<Drawable name = "color name"> # color value </drawable>
</Resources>
2. Reference in XML
<Textview
....................
Android: textcolor = "@ drawable/darkgray"
Android: Background = "@ drawable/White"
.....................
/>
In string reference:
Android: Tex = "@ string/string name"
XML definition
Strings. xml
<Resources>
<String name = "string name"> string </string>
</Resources>
We can find that the definition formats of these resources are different from those of references only when the elements in the XML file are different,
You do not need to call the file name when using it, because these elements appear directly in the form of IDs in the r file.
3. Reference in the program
Drawable = resource. getdrawable (R. drawable. White );
Tw. setbackgrounddrawable (drawable );
4. Here are three ways to set the background color of textview.
Void setbackgroundcolor (INT color );
Void setbackgrounddrawable (drawable D );
Void setbackgroundresource (INT resid );
------------------------------------------------
Tw. setbackgrounddrawable (drawable); // reference the Custom Resource file.
Tw. setbackgroundcolor (color. White); // The values in the color class are directly used here.
Tw. setbackgroundresource (R. drawable. White); // This is called as an ID.