Original URL: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0924/1712.html
The following code should be familiar to many people:
123456 |
<?xml version=
"1.0" encoding=
"utf-8"
?>
<selector xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<item android:drawable=
"@drawable/numpad_button_bg_selected" android:state_selected=
"true"
></item>
<item android:drawable=
"@drawable/numpad_button_bg_pressed" android:state_pressed=
"true"
></item>
<item android:drawable=
"@drawable/numpad_button_bg_normal"
></item>
</selector>
|
This is a background selection for button use, this different state shows different backgrounds of the XML file we call selector. In fact, the essence of selector is a drawable object.
What if you want to implement the above selector with Java code? The answer is statelistdrawable.
Let's take a look at one of the simplest examples of implementing statelistdrawable:
1234 |
StateListDrawable drawable = new StateListDrawable(); drawable.addState( new int[]{android.R.attr.state_focused}, mFocusedDrawable); drawable.addState( new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, mEnabledPressedDrawable); drawable.addState( new int[0], mDefaultDrawable); |
In this code we have added three states.
Let's show you how to use Statelistdrawable in Java and her corresponding XML notation with more canonical code:
First define a method to get the Statelistdrawable object:
123456789101112131415 |
private StateListDrawable addStateDrawable(Context context, int idNormal, int idPressed, int idFocused) {
StateListDrawable sd =
new StateListDrawable();
Drawable normal = idNormal == -1 ?
null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == -1 ?
null : context.getResources().getDrawable(idPressed);
Drawable focus = idFocused == -1 ?
null : context.getResources().getDrawable(idFocused);
//注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
//所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了
sd.addState(
new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
sd.addState(
new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
sd.addState(
new int[]{android.R.attr.state_focused}, focus);
sd.addState(
new int[]{android.R.attr.state_pressed}, pressed);
sd.addState(
new int[]{android.R.attr.state_enabled}, normal);
sd.addState(
new int[]{}, normal);
return sd;
}
|
The order in which the comments are made is addState
important.
UseddStateDrawable
123 |
//……前面对Button的声明略去 okBtn.setBackgroundDrawable(addStateDrawable( this , R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected)); cancelBtn.setBackgroundDrawable(addStateDrawable( this , R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected)); |
The XML approach is implemented:
The following corresponds to the specific instance, because it is done in the background, the XML will be placed under/res/drawable (StateList in the first match the current state of the item will be used.) Therefore, if the first item does not have any state attributes, it will be used every time, which is why the default value must always be the last one.
Definition of selector:
1234567 |
<?xml version=
"1.0" encoding=
"utf-8"
?>
<selector xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<item android:state_pressed=
"true" android:drawable=
"@drawable/btn_selected"
/>
<item android:state_focused=
"true" android:drawable=
"@drawable/btn_selected"
/>
<item android:state_enabled=
"true" android:drawable=
"@drawable/btn_normal"
/>
<item android:drawable=
"@drawable/btn_normal" />
</selector>
|
Using Selector
1234567891011 |
<Button
android:id=
"@+id/canel"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"@string/btn_cancel"
android:layout_margin=
"10dip"
android:layout_weight=
"1"
android:textColor=
"#ffffffff"
android:textSize=
"15sp"
android:background=
"@drawable/button_drawable"
/>
|
Application of selector-statelistdrawable in "Go" Java code to implement Android background selection