自定义按钮不显示其drawable

时间:2013-10-09 20:08:51

标签: android android-layout

我有一个自定义按钮button.xml。默认(未按下)drawable为button_unclicked.xml,单击按钮时会调用button_clicked.xml。问题是按钮不显示其单击和未单击的drawable,但只显示按钮内的文本(没有边框)。

我将自定义按钮设置为背景:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/apply"
    android:id="@+id/button_apply"
    android:onClick="Apply"
    android:layout_marginBottom="10dp"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/button" />

button_clicked:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#FFFFFF"
        android:endColor="#FFFFFF"
        android:angle="270" />
    <corners android:radius="3dp" />
    <stroke android:width="5px" android:color="#FFE75C" />
</shape>

button_unclicked:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#FFFFFF"
        android:endColor="#FFFFFF"
        android:angle="270" />
    <corners android:radius="3dp" />
    <stroke android:width="5px" android:color="#000000" />
</shape>

</selector>

button.xml:     

    <item android:state_pressed="true"
    android:drawable="@drawable/button_clicked">
</item>

<item android:state_pressed="false"
    android:drawable="@drawable/button_unclicked">
</item>

输出(应该有边框,但没有): enter image description here

2 个答案:

答案 0 :(得分:1)

请尝试以下模板:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_default_normal_holo_light" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="@drawable/btn_default_disabled_holo_light" android:state_enabled="false" android:state_window_focused="false"/>
    <item android:drawable="@drawable/btn_default_pressed_holo_light" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_default_focused_holo_light" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_default_normal_holo_light" android:state_enabled="true"/>
    <item android:drawable="@drawable/btn_default_disabled_focused_holo_light" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_default_disabled_holo_light"/>

</selector>

您不必放置所有<item>标记。

答案 1 :(得分:1)

// try this 
drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="false" >
        <shape
                android:shape="rectangle" >
            <gradient android:startColor="#FFFFFF"
                      android:endColor="#FFFFFF"
                      android:angle="270" />
            <corners android:radius="3dp" />
            <stroke android:width="5px" android:color="#000000" />
        </shape>
    </item>
    <item android:state_enabled="true" android:state_focused="false" android:state_pressed="false" >
        <shape  android:shape="rectangle" >
            <gradient android:startColor="#FFFFFF"
                      android:endColor="#FFFFFF"
                      android:angle="270" />
            <corners android:radius="3dp" />
            <stroke android:width="5px" android:color="#FFE75C" />
        </shape>
    </item>
    <item android:state_pressed="true" >
        <shape
                android:shape="rectangle" >
            <gradient android:startColor="#FFFFFF"
                      android:endColor="#FFFFFF"
                      android:angle="270" />
            <corners android:radius="3dp" />
            <stroke android:width="5px" android:color="#000000" />
        </shape>
    </item>
    <item android:state_focused="true" >
        <shape
                android:shape="rectangle" >
            <gradient android:startColor="#FFFFFF"
                      android:endColor="#FFFFFF"
                      android:angle="270" />
            <corners android:radius="3dp" />
            <stroke android:width="5px" android:color="#000000" />
        </shape>
    </item>
</selector>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Button"
        android:padding="5dp"
        android:background="@drawable/selector"/>