问题
this is my xml drawbale code with the name of btntheme.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/customactionbartheme_btn_default_normal_holo_light" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@drawable/customactionbartheme_btn_default_disabled_holo_light" android:state_enabled="false" android:state_window_focused="false"/>
<item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_pressed="true"/>
<item android:drawable="@drawable/customactionbartheme_btn_default_focused_holo_light" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/customactionbartheme_btn_default_normal_holo_light" android:state_enabled="true"/>
<item android:drawable="@drawable/customactionbartheme_btn_default_disabled_focused_holo_light" android:state_focused="true"/>
<item android:drawable="@drawable/customactionbartheme_btn_default_disabled_holo_light"/>
</selector>
I've put all those images with the these names in the drawable-hdpi folder .
this is my layout and the button :
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="@drawable/btntheme"
android:textSize="@dimen/font_size_smallerButton" />
it's not working , it doesn't change the button theme at all ?
could you help me to solve it ?
thank you
回答1:
Try out selector as below:
<!-- Active tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_normal_holo_light" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<!-- Pressed tab -->
<item android:drawable="@drawable/customactionbartheme_btn_default_pressed_holo_light" android:state_pressed="true"/>
<!-- Selected tab (using d-pad) -->
<item android:drawable="@drawable/customactionbartheme_btn_default_disabled_holo_light" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
</selector>
For more detail guidenc check out Custom Selector in Android
回答2:
try to add <?xml version="1.0" encoding="utf-8"?>
in xml file and xml file will like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_selected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_unselected_focused" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/custom_tab_indicator_selected_focused" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_unselected_pressed" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_selected_pressed" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_unselected_pressed" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/custom_tab_indicator_selected_pressed" />
</selector>
after using this if it will not work then if possible try to use image view instead of button i think that will be work. thanks;-)
回答3:
The problem might be that the screen you are testing on does not use drawable-hdpi, so try to move the images and xml file itself to the drawable folder, if it does not exist then create it.
回答4:
Create drawable folder in res and put btntheme.xml in that folder .
回答5:
Ok I also suffered form this problem, remove android:state_focused="true" line form your code and run. hope get the answer :), (i think focus is only for input type views in my knowledge if i wrong plz guide me) for testing focus state press TAB key this is sample:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--When pressed-->
<item
android:state_pressed="true"
android:drawable="@drawable/btn_bg" />
<!--focus state -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/colorAccent"/>
<!--Default State-->
<item
android:state_focused="false"
android:state_pressed="false"
android:drawable="@color/transparent_black" />
<!-- how confirm focus state of button -->
</selector>
来源:https://stackoverflow.com/questions/24624038/android-button-background-drawable-doesnt-work