是否可以使用带有可绘制对象和<shape>属性的 XML 背景资源?
所以我有这个按钮
<Button
android:layout_marginRight="5dp"
android:id="@+id/send_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/send_button" />
Run Code Online (Sandbox Code Playgroud)
其中有一个后台send_button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_send_white_48dp" android:state_focused="true"/>
<item android:drawable="@drawable/ic_send_white_48dp" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_send_black_48dp"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
现在效果很好。但我还想在可绘制和圆角后面添加背景颜色,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="4dp" />
<gradient
android:angle="270"
android:endColor="#88b823"
android:startColor="#b0dc54" />
</shape>
Run Code Online (Sandbox Code Playgroud)
那么是否可以将这两个 XML 资源结合起来呢?到目前为止我所尝试的只是显示可绘制对象而不是形状:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_send_white_48dp" android:state_focused="true"/>
<item android:drawable="@drawable/ic_send_white_48dp" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_send_black_48dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="4dp" />
<gradient android:angle="270"
android:endColor="#88b823"
android:startColor="#b0dc54" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
感谢@CommonsWare 为我指明了正确的方向。这是工作代码(当前仅适用于默认状态):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_send_white_48dp" android:state_focused="true"/>
<item android:drawable="@drawable/ic_send_white_48dp" android:state_pressed="true"/>
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="4dp" />
<solid android:color="@color/action_bar" />
</shape>
</item>
<item>
<bitmap android:src="@drawable/ic_send_black_48dp" />
</item>
</layer-list>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9653 次 |
| 最近记录: |