Want to use LinearLayout as a button, plus a dynamic background, when pressed, the background color, this should be taken for granted using the selector background selector to do:
<LinearLayoutAndroid:id= "@+id/btn_user_profit_record"Android:layout_width= "0DP"Android:layout_height= "130DP"Android:layout_weight= "1"Android:background= "@drawable/bg_selector"android:gravity= "Center"android:orientation= "vertical"> </LinearLayout>
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@color/white"/> <Itemandroid:drawable= "@color/white_dark"android:state_pressed= "true"/></selector>
After this writing, dead and alive have no effect, has not changed color, after many attempts to find that someone wrote a similar selector is the above line is set to Stat_pressed= "false", I tried a bit, really effective
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@color/white"android:state_pressed= "false"/> <Itemandroid:drawable= "@color/white_dark"android:state_pressed= "true"/></selector>
A deeper layer of thinking, why is this so? Set a default state, should be feasible, do not need to be all indicated, so that it is not related to the order, as a selector, may be in accordance with the Order of judgment, then every time the state change to judge the time, must be satisfied with the first (because of what conditions are not), So the rest of it won't come into effect, so I made the changes:
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@color/white_dark"android:state_pressed= "true"/> <Itemandroid:drawable= "@color/white"/></selector>
By moving the default state to the bottom, it really works, which proves that I was right about the idea.
I did not think that the project in selector will have a sequence (not how carefully read the official documents, my pot), it seems later for the more complex selector how to sort should be careful.
In addition before the online search time to see someone pointed out, for layout to specify clickable= "true", only "activation" selector to take effect, I also by the way to try, seemingly did not set this property is not related to the same effect.
The "original" Android selector selector is invalid or does not display properly for a little research