9

我想为按钮使用以下选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/jobs" android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
        </shape>
<scale android:scaleHeight="90%" android:scaleWidth="90%" />
    </item>
    <item android:drawable="@drawable/jobs"></item>

</selector>

但它不起作用。我想用相同的drawable使按钮的角变圆并缩小10%。实际上,我想使用单个可绘制对象来提供按钮按下效果。可能吗?

4

1 回答 1

21

我发现最好将状态逻辑和可绘制代码分开。来自 Android 文档:http: //developer.android.com/guide/topics/resources/drawable-resource.html#StateList

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

然后,我会将代码放在单独的可绘制 XML 中以提供圆角。我不确定您是否甚至可以直接在selector.

于 2012-08-08T14:32:09.493 回答