android-button背景drawable不起作用

时间:2014-07-08 05:01:48

标签: android android-button android-theme

这是我的xml drawbale代码,名称为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>

我已将所有带有这些名称的图像放在drawable-hdpi文件夹中。

这是我的布局和按钮:

<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" />

它不起作用,它根本不会改变按钮主题?

你可以帮我解决一下吗?

谢谢

7 个答案:

答案 0 :(得分:2)

尝试在xml文件中添加<?xml version="1.0" encoding="utf-8"?>,xml文件将

<?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>

使用它后,如果它不起作用,那么尽可能尝试使用图像视图而不是按钮我认为这将是有效的。感谢; - )

答案 1 :(得分:1)

尝试选择如下:

<!-- 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>

有关更详细的指导,请查看Custom Selector in Android

答案 2 :(得分:1)

问题可能是您正在测试的屏幕不使用drawable-hdpi,因此尝试将图像和xml文件本身移动到drawable文件夹,如果它不存在则创建它。

答案 3 :(得分:1)

问题采用了样式中提到的材料设计 简单地替换为

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">

答案 4 :(得分:1)

导航到res/values/styles/styles.xmlres/values/themes/themes.xml 并将Theme.MaterialComponents.Light.NoActionBar更改为Theme.AppCompat.Light.NoActionBar

或仅将MaterialComponents替换为AppCompat

答案 5 :(得分:-1)

res 中创建 drawable 文件夹,并将 btntheme.xml 放在该文件夹中。

答案 6 :(得分:-1)

好吧,我也遇到了这个问题, 从代码中删除android:state_focused =“ true”行并运行。 希望得到答案:), (我认为如果我错了,请引导我就只关注输入类型的视图) 要测试焦点状态,请按TAB键 这是示例:-

<?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>