custom button: change style when pressed
I created button with some shadow using a style:
<style name="test">
<item name="android:shadowColor">#FFFFFF</item>
<item name="android:shadowRadius">1</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
</style>
This applies a white shadow on the button's text in its normal state. I was just wondering if anyone knows if there is a way to remove this shadow when the button is a pressed state. In other words, is there a way to apply another style when the button is in another (pressed) state?
Thanks in advance!
edit
bold.xml:
<resources>
<style name="bold_text">
<item name="android:textStyle">bold</item>
</style>
</resources>
button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/test_pressed"
style="@style/bold_text"
android:state_pressed="true"/>
<item android:drawable="@drawable/test_focused"
android:state_focused="true"/>
<item android:drawable="@drawable/test_normal"/>
</s开发者_如何学运维elector>
my layout:
<Button
...
android:background="@drawable/button"/>
You want a ColorStateList http://developer.android.com/guide/topics/resources/color-list-resource.html
After a review of one of my button.xml files, I found that the order of evaluation (which is noted in the documents..) was such that my testing chose the same drawable. Correcting the order made it work.
Except not in the case of button2.xml. I made it work by removing all except the "drawable" and the "state_*" specifications. I had a "textColor" in there - looks like XML that gets past other checks (as does style=".." above) silently invalidates the selector. A good place to extend validation!
Schwiz has it right... for text use a separate selector defined in the "color" subdirectory of your resource directory: see ColorStateList.
精彩评论