Android button background selector

旧时模样 提交于 2019-12-01 15:14:09

问题


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>

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?


回答1:


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>

I would then put the code to give rounded corners in a separate drawable XML. I'm not sure if you can even do such things directly in a selector.



来源:https://stackoverflow.com/questions/11866623/android-button-background-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!