This article describes the Android programming implementation of the control of different state text display different colors of the method. Share to everyone for your reference, specific as follows:
Mode one :
The first control to select
<textview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
@+id/close_time_display "
android:layout_marginright=" 20DP "
android:text=" @string/default_time "
style= "@style/item_content_text_style"/>
Style is a custom style, and the corresponding XML file is as follows:
<style name= "Item_content_text_style" >
<item name= "android:textsize" >26sp</item>
<item name= "Android:duplicateparentstate" >true</item>
<item name= "Android:textcolor" >@ Drawable/textcolor_yellow_selector</item>
</style>
The Textcolor_yellow_selector in TextColor is as follows
<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/" Res/android "
>
<item
android:state_pressed=" true "
android:color=" @color/yellow "/>
<item
android:state_focused= "true"
android:color= "@color/yellow"/>
<item android: State_selected= "true"
android:color= "@color/yellow" ></item>
<item android:color= "@color/ White "/>
</selector>
Implementation mode two : colorstatelist text color
Api
Windows platform VC, for different button states, using a different color display text, to achieve more complex, generally have to draw a button. But Android is easy to implement.
We first add a colorstatelist resource XML file, which is saved in Res/color/button_text.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<item android:state_pressed=" true "
android:color=" #ffff0000 "/> <!--pressed-->
<item android:state_focused= "true"
android:color= "#ff0000ff"/> <!--focused-->
<item Android:color= "#ff000000"/> <!--default-->
</selector>
Button btn= (button) Findviewbyid (R.ID.BTN);
Resource= (resources) Getbasecontext () getresources ();
Colorstatelist csl= (colorstatelist) resource.getcolorstatelist (r.color.button_text);
if (csl!=null) {
btn.settextcolor (color_state_list);//Set button text color
}
Or you can do this:
Xmlresourceparser Xpp=resources.getsystem (). GETXML (R.color.button_text);
try {
colorstatelist csl= colorstatelist.createfromxml (getresources (), xpp);
Btn.settextcolor (CSL);
} catch (Exception e) {
//Todo:handle Exception
}
Finally attach any states that may appear:
<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android >
<item
android:color= "Hex_color"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_active=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"]/>
</selector>
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage
I hope this article will help you with the Android program.