Android Practical tips-using code to implement selector

Source: Internet
Author: User

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.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.