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>

我应该添加什么来使状态成为焦点并按下?按下状态的渐变颜色,以及聚焦状态的另一种颜色?

4

3 回答 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 设置为按钮背景

于 2013-10-03T12:00:49.980 回答
2

请参阅本教程,了解如何创建适当的 drawable-xml 文件以允许按钮的不同状态:

http://undertowsam.wordpress.com/2012/04/23/design-custom-background-and-button-for-android-using-xml/

于 2013-10-03T11:58:47.610 回答
2

创建button_selector.xml内部drawable文件夹如下:

<?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,一个用于按下,另一个用于聚焦,正如您已经创建的那样image_pressed.xmlimage_focused.xml

于 2013-10-03T12:01:30.933 回答