When we define a drawable, we can use the drawable object defined by xml. It enables an image to display different patterns in different states, such as a Button, which has a pressed, focused, or other state, and by using the state list drawable, you can provide a different image for each state.
E.g:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/efax_tab_inbox_disabled" android:state_enabled="false"/> <item android:drawable="@drawable/efax_tab_inbox_p" android:state_selected="true"/> <!--This will switch to another image when pressed--!> <item android:drawable="@drawable/efax_tab_inbox_p" android:state_pressed="true"/> <item android:drawable="@drawable/efax_tab_inbox"/> </selector>
The properties are explained as follows:
android:drawable put a drawable resource
android:state_pressed whether it is pressed, such as a button touch or click.
android:state_focused Whether to get the focus, such as the user selects a text box.
android:state_hovered Whether the cursor is hovering, usually the same as the focused state, it is a new feature of 4.0
android:state_selected is selected, it is not exactly the same as the focus state, such as when a list view is selected, the various subcomponents in it Possibly through the arrow keys, was selected.
android:state_checkable Whether the component can be checked. Such as: RadioButton can be checked.
android:state_checked is checked, such as: a RadioButton can be checked.
android:state_enabled can accept touch or click events
android:state_activated is activated (for example, this trouble is not very clear)
android:state_window_focused whether the application is in the foreground, when the notification bar is pulled down or a dialog box pops up when the application not at the front desk
Note: If there are multiple items, the program will automatically match from top to bottom, and the first matching item will be applied. (Not by best match)
If an item doesn't have any state specification, then it can be matched by any state.
You can set the state of the button in the activity
mBtn_efax_received.setSelected(true); mBtn_efax_failed.setSelected(false); mBtn_efax_sent.setSelected(false); mBtn_efax_recycle.setSelected(false);
Set the number of small dot messages to prompt:
<ImageButton android:id="@+id/imgbtn_inbox" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="2dp" android:background="@drawable/selector_efax_tab_inbox" /> <TextView android:id="@+id/efax_main_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_alignParentTop="true" android:layout_marginTop="10dp" android:background="@drawable/indicator_xml" android:gravity="center_horizontal" android:text="88" android:textColor="@color/white" android:textIsSelectable="false" android:textSize="15sp" android:visibility="gone" /> </RelativeLayout>
Create a new Drable
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android= "http://schemas.android.com/apk/res/android" android:shape= "oval" android:useLevel= "false" > <solid android:color= "#c6001d" /> <stroke android:width= "1dp" android:color= "#c6001d" /> <size android:width= "20dp" android:height= "20dp" /> </shape>
参考:
http://www.oschina.net/question/920274_212245