Android的按键背景选择 [英] Android button background selector
问题描述
我想用下面的选择器按钮:
I want to use the following selector for button:
<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>
不过,这是行不通的。 我要让按键边角圆和10%的面积较小,相同的绘制。其实我想用单一的绘制给一个按钮pressed效果。这可能吗?
But it does not work. I want to make buttons corners round and 10% small in size with the same drawable. Actually I want to give a button pressed effect using single drawable. Is it possible?
推荐答案
我觉得最好分开的状态逻辑和绘制code。从Android文档:<一href="http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList">http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
I find it best to separate the state logic and drawable code. From the Android docs: 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>
我会再把code给圆角在一个单独绘制的XML。我不知道,如果你甚至可以直接在选择做这样的事情
。
这篇关于Android的按键背景选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!