Android RelativeLayout Selector state_pressed doesn't work

给你一囗甜甜゛ 提交于 2019-12-04 23:21:15

make sure your RelativeLayout is clickable

You could also set android:addStatesFromChildren="true" in your RelativeLayout instead of the android:clickeable="true". If your children are already clickeable, focusable, etc. You shouldn't make your RelativeLayout clickeable or focusable.

Try to add to your ImageView android:clickable="true"

In my opinion you should use Button and create selector for it instead of making custom button by creating RelativeLayout and putting there ImageView and TextView. Then you can use android:drawableTop="@drawable/your_contact_icon". Afterwards you can check if your selector works fine.

hope these helps someone;

  1. make sure view clickable. descendants can block click event. more info search the below attributes.

android:clickable, android:descendantFocusability, android:focusable, android:focusableInTouchMode

  1. in style xml you should define item element state attributes or make sure the item with no attribute must be at the end.

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/lightGold"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/white_two"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
    

But this sample will not work;

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <solid android:color="@color/white_two"/>
        <stroke android:width="1dp" android:color="@color/lightGrey"/>
        <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
    </shape>
</item>
<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/lightGold"/>
        <stroke android:width="1dp" android:color="@color/lightGrey"/>
        <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
    </shape>
</item>

or you can define state attribute for both item. So order will be not important.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape>
            <solid android:color="@color/white_two"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/lightGold"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
</selector>

Ref;

During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state

State List https://developer.android.com/guide/topics/resources/drawable-resource.html

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