Android: how to use selector?

后端 未结 3 1430
日久生厌
日久生厌 2020-12-21 23:07

I have a problem of using selector that it does not work as what I expect. I wanna click on it then it gives me reaction and I select it(By long click but I probably do it t

相关标签:
3条回答
  • 2020-12-21 23:20

    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

    0 讨论(0)
  • 2020-12-21 23:27

    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.

    0 讨论(0)
  • 2020-12-21 23:38

    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"

    0 讨论(0)
提交回复
热议问题