This article describes the method of Android programming to display different colors of text in different states of controls. Share it for your reference, as follows:
Method one:
The first control to choose
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android: 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>
Textcolor_yellow_selector in textColor is as follows
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:andro > <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 method two: ColorStateList text changes color
API
Windows platform VC uses different colors to display text for different button states, which is more complicated to implement and generally requires self-drawing of buttons. But it is very convenient to implement in Android.
We first add a ColorStateList resource XML file, and the XML file is saved in res/color/button_text.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:andro> <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(); Resources resource=(Resources)getBaseContext().getResources(); ColorStateList csl=(ColorStateList)(.button_text); if(csl!=null){ (color_state_list);//Set the button text color}
Or it can be:
XmlResourceParser xpp=().getXml(.button_text); try { ColorStateList csl= (getResources(),xpp); (csl); } catch (Exception e) { // TODO: handle exception }
Finally, all possible statuses are attached:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:andro > <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 about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.