Android custom Drawable resource reference problem, androiddrawable

Source: Internet
Author: User

Android custom Drawable resource reference problem, androiddrawable
Recurrence of problems:

The code for the Activity layout file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "match_parent"> <RelativeLayout android: clickable = "true" android: layout_centerInParent = "true" android: layout_width = "100dp" android: layout_height = "54dp" android: background = "@ drawable/color_bg_selector"> <TextView android: layout_centerInParent = "true" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "25sp" android: textColor = "@ color/bg_color_text_selector" android: text = "OK"/> </RelativeLayout>

For a button View, the background can be changed, and the font color of TextView can also be changed.

Color_bg_selector.xml code:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true">        <shape android:shape="rectangle">            <corners android:radius="5dp"/>            <stroke android:color="@color/btn_color_normal"                android:width="3dp"/>            <solid android:color="@android:color/white"/>        </shape>    </item>    <item android:state_selected="true">        <shape android:shape="rectangle">            <stroke android:color="@color/btn_color_normal"                android:width="3dp"/>            <solid android:color="@android:color/white"/>            <corners android:radius="5dp"/>        </shape>    </item>    <item >        <shape android:shape="rectangle">            <stroke android:color="@color/btn_stoke_druck"                android:width="3dp"/>            <solid android:color="@android:color/white"/>            <corners android:radius="5dp"/>        </shape>    </item></selector>

The bg_color_text_selector.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/txt_color_normal"/>    <item android:state_selected="true" android:color="@color/txt_color_select"/>    <item android:state_pressed="true" android:color="@color/txt_color_select"/></selector>

There is only one button in the code to change the background color of the button when you click it, And the font color also needs to be changed, that is, the expected effect is:

Yes, that's right. This is just the expected result. Reality is often different from expectation.
We run the code and the result is as follows:

If you have any questions, you can copy the above Code to the project and try it!
It is true that the font color is not changed when it is pressed. why? Why? Why? Why?
Set the font color

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/txt_color_normal"/>    <item android:state_selected="true" android:color="@color/txt_color_select"/>    <item android:state_pressed="true" android:color="@color/txt_color_select"/></selector>

I once doubted whether the font color cannot be set like this? Finally, I changed it to the same sequence as above bg_color_text_selector.xml.

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_selected="true" android:color="@color/txt_color_select"/>    <item android:state_pressed="true" android:color="@color/txt_color_select"/>    <item android:color="@color/txt_color_normal"/></selector>

I wiped it. It worked!
Here we can draw a conclusion: The reading order of drawable is related to the reading order!
How can this situation be avoided?
There are two methods:
1. Do not be lazy. Write all the changes completely.

<item android:state_pressed="false" android:color="@color/txt_color_normal"/>

Android: state_pressed = "false" is omitted, resulting in no effect.

2. Do not write the unwanted status in it, for example, here

android:state_selected="true"

It's useless, so don't write it in,

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/txt_color_normal"/>    <item android:state_pressed="true" android:color="@color/txt_color_select"/></selector>

This is also effective.

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.