自定义XML按钮

3

我制作了这个XML文件来自定义一个按钮

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

<corners android:radius="10dip" />

<gradient
    android:angle="90"
    android:centerColor="#0043E1"
    android:centerY="0.4"
    android:endColor="#6495ED"
    android:startColor="#6495ED"
    android:type="linear" />

</shape>

我应该添加什么来使状态聚焦和按下?为状态按下添加渐变颜色,为状态聚焦添加另一个渐变色。

3个回答

4
你需要为按钮编写选择器。
<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_normal_color" android:state_focused="true"></item>
    <item android:drawable="@drawable/button_focus_color" android:state_pressed="true">/item>
    <item android:drawable="@drawable/button_normal_color"></item>
  </selector>

将此xml设置为按钮的背景


2

2
drawable文件夹中创建如下的button_selector.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/image_normal" />
<item android:state_focused="false"
        android:state_pressed="true" android:drawable="@drawable/image_pressed" />
<item android:state_focused="true"
        android:state_pressed="false" android:drawable="@drawable/image_focused" />
</selector>

然后创建两个xml文件,一个用于pressed状态,另一个用于focused状态,就像您已经创建的一样,命名为image_pressed.xmlimage_focused.xml


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接