0

我有一个自定义按钮button.xml。它的默认(未按下)drawable 是button_unclicked.xml并且在button_clicked.xml单击按钮时被调用。问题是按钮不显示其单击和未单击的可绘制对象,而仅显示按钮内的文本(没有边框)。

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

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

按钮.xml:

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

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

输出(应该有边界但没有): 在此处输入图像描述

4

2 回答 2

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>标签。

于 2013-10-10T02:53:18.913 回答
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"/>
于 2013-10-10T04:07:35.577 回答