Android – how do I set different themes for button selectors?
•
Android
How to set different styles for button selectors according to the current application theme?
This is my button_ selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color android:color="@color/color_theme1"/>
</item>
<!-- pressed -->
<item android:drawable="@color/transparent"/>
<!-- default -->
</selector>
resolvent:
Because your application theme color is located in color.xml_ You can use it in your selector. But you must create two drawables files, one for the default state and the other for selected_ state.
button_ selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--selected/pressed/focused -->
<item android:state_selected="true"
android:drawable="@drawable/button_selected"
/>
<item android:drawable="@drawable/button_default"/>
<!-- default -->
</selector>
button_ default.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!--this is to give gradient effect -->
<gradient android:angle="270"
android:startColor="@color/gray"
android:endColor="#@color/gray"
/>
<!-- this will make corners of button rounded -->
<corners android:topLefTradius="5dip"
android:bottomRighTradius="5dip"
android:topRighTradius="5dip"
android:bottomLefTradius="5dip"/>
</shape>
button_ selected.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!--this is to give gradient effect -->
<gradient android:angle="270"
android:startColor="@color/color_primary"
android:endColor="#@color/color_primary"
/>
<!-- this wil make corners of button rounded -->
<corners android:topLefTradius="5dip"
android:bottomRighTradius="5dip"
android:topRighTradius="5dip"
android:bottomLefTradius="5dip"/>
</shape>
You must also programmatically do the following in order for the button to remain selected
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.isSelected())
{
v.setSelected(false);
}
else
{
v.setSelected(true);
}
}
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码