The "original" Android selector selector is invalid or does not display properly for a little research

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.