Android: how to use selector?

我的未来我决定 提交于 2019-11-29 17:00:34

Thank you to all brothers trying hard to answer me. I have got the answer....just simply by setting android:clickable="true" at LinearLayout. Ha, it's silly.....

I remember dude who provide relating information but I ignored... sorry to him.

Tushar Gogna

Create an XML file and place it in the drawable folder. Open it and write the following code:

<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item 
        android:drawable="YOUR IMAGE OR COLOR"

         android:state_pressed="true"/>
    <item
        android:drawable="YOUR IMAGE OR COLOR"

          android:state_selected="true"/>
    <item
        android:drawable="YOUR IMAGE OR COLOR"

           android:state_focused="true"/>

</selector>

Then in your main XML file, place android:background="@drawable/selector.xml"

What are you expecting? A reaction only when it's clicked?

Try removing removing:

android:state_selected="true" 

Edit: This should be your selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/my_drawable" android:state_selected="true"></item>
    <item android:drawable="@drawable/my_drawable" android:state_pressed="true"></item>
    <item android:drawable="@drawable/my_drawable"></item>

</selector>

What is the behaviour that you are experiencing?

Edit:

<LinearLayout 
     android:layout_weight="9"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:background="@drawable/selector_row"
     android:duplicateParentState=true>

I don't know if it actually solves the problem

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