How to replace drawable source of a xml item
I have a drawable selector(home_button.xml) for a button, like...
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/blue_button" />
<item android:state_pressed="true"
android:drawable="@drawable/red_button" />
</selector>
And in my main activity's layout i'm using this drawable as
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home_button" android:text="@string/home_button"
android:background="@drawable/home_button" ></Button>
Now i need to display dynamic remote images in place of local blue_button.png and red_button.png of selector xml inside activity based on the user login.
How to d开发者_StackOverflow社区o this?
On the Android Button class Documentation you get:
If you're not satisfied with the default button style and want to customize it to match the design of your application, then you can replace the button's background image with a state list drawable.
You might want to replace the image button background drawable with an StateListDrawable you like in this SO post, assuming that you have downloading both images and have them as drawable objects already
StateListDrawable states = new StateListDrawable();
//This for pressed true
states.addState(new int[] {android.R.attr.state_pressed},
drawable_image1);
//This for pressed false
states.addState(new int[] { },
drawable_image2);
//Change it on the button
button.setBackgroundDrawable(states);
Use a BitmapDrawable.
If you create another drawable just set a new one in code:
public void setBackgroundResource (int resid)
Button is a View so this should work fine. More info.
If you need to change a 'Selector' take into account that a selector is just a StateListDrawable. and so you could just create one programnatically adding the states as you need them.
Add your style
<style name="MyButtonStyle" parent="android:Widget.Button">
<item name="android:background">@drawable/btn_default</item>
</style>
and your selector (here btn_default.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_normal" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_pressed" />
<item
android:drawable="@drawable/btn_default_normal" />
</selector>
and your images (9 patch) btn_default_normal and btn_default_pressed (the names can be different than these) Then, apply the style to your button in xml:
<Button
...
style="@style/MyButtonStyle"
/>
精彩评论