在Android上将具有可绘制背景背景的按钮设为“按下”状态?

时间:2018-07-17 20:05:42

标签: android android-layout

大家晚上好!

有没有一种方法可以使我的按钮看起来像被按下而不必设计每个按钮两次?(一个用于按下状态,另一个用于不按下状态)。我使用http://angrytools.com/android/button/设计了按钮的形状,并将这些形状用作按钮的背景。我听说过ImageButton,但它似乎不适用于这里,因为我需要按钮具有文本。

我的背景形状之一的示例:

enter image description here

非常感谢!

1 个答案:

答案 0 :(得分:4)

尝试此代码,将此XML文件设置为按钮的背景。

but_background.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape android:shape="rectangle">
                <solid android:color="#9df20901" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
              <solid android:color="#892379" />
            </shape>
        </item>
    </selector>
相关问题