As is known to all, Android can create selector from an XML file and install it as a Drawable object to provide a unified style setting. But at some point, we need to implement the same function in the form of code, for example, the number of components is very large, corresponding to different pictures, if you still use XML, you need to create a lot of selector files, very cumbersome.
For example, a textview uses the following selector
<textview android:id= "@+id/textview_title" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:focusable= "true" android:drawabletop= "@drawable/selector_tabwidget_icon" Android:textalignment= "center" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;/>
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http// Schemas.android.com/apk/res/android " ><!-- non focused states --><item android:state_focused= "false" android:state_selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact" /><item android:state_focused= "false" android: State_selected= "true" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" / ><!-- focused states --><item android:state_focused= "true" android:state_ Selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <item android:state_focused= "true" android:state_selected= "true" android:state_pressed= " False " android:drawable=" @drawable/contact_sel " /><!-- Pressed --><item Android:state_selected= "true" androiD:state_pressed= "true" android:drawable= "@drawable/contact_sel" /><item android:state_ Pressed= "true" android:drawable= "@drawable/contact_sel" /></selector>
There are a lot of pictures referenced in the file, if each file corresponds to an XML file, it will be very cumbersome, very cumbersome to modify.
In fact, all the XML settings can be done, Android can also be implemented in a coded way, like the above XML file, you can implement the following code:
Statelistdrawable drawable = new statelistdrawable (); //non focused states drawable.addstate ( New int[]{-android. R.attr.state_focused, -android. R.attr.state_selected, -android. r.attr.state_pressed}, getresources (). getdrawable (r.drawable.contact)); Drawable.addstate (new int[]{-android. R.attr.state_focused, android. R.attr.state_selected, -android. r.attr.state_pressed}, getresources (). getdrawable (R.drawable.contact_sel)); //focused states drawable.addstate (new int[]{ Android. R.attr.state_focused,-android.R.attr.state_selected, -android. r.attr.state_pressed}, getresources (). getdrawable (R.drawable.contact_sel)); drawable.addstate (new int[]{android. R.attr.state_focused,android. R.attr.state_selected, -android. r.attr.state_pressed}, getresources (). getdrawable (R.drawable.contact_sel)); //pressed drawable.addstate (new int[]{android. R.attr.state_selected, android. r.attr.state_pressed}, getresources (). getdrawable (R.drawable.contact_sel)); drawable.addstate (new int[]{android. r.attr.state_pressed}, getresources (). Getdrawable (R.drawable.contact_sel)); TextView textView = (TextView) findviewbyid (r.id.textview_ title); textview.setcompounddrawableswithintrinsicbounds (null, drawable , null, null);
Note that the "-" in the "-" number, when the XML is set to False, it is necessary to use the negative value of the resource symbol to set.