Android Custom drawable Resource Reference issues

Source: Internet
Author: User

The recurrence of the problem:

The Activity layout file code 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< /span>= "100DP"  android:layout_height  = "54DP"  android:background  =  "@drawable/color_bg_selector" ;         <TextViewandroid: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></relativelayout>

A button view, the background can be changed, TextView font color 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>

The code has only one button, which changes the background color of the button when clicked, and the font color also changes, that is, the effect we expect is:

Yes, that's right, it's just the effect we expect, the reality is often the same as expectations.
We run the code and the result is this:

If you have any doubts, you can copy the above code into the project and run and try it!
It does not change when the color of the font is pressed, why? Why? Why? Why?
Clearly set the font color to

<?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 also once wondered if the font color could not be set like this? Finally, I changed it to the same order as the 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 rub it, it works!
Here you can conclude that drawable is related to order when reading state!
How can we avoid this situation?
There are two ways of doing this:
1, do not lazy, rewrite all write, such as here is clearly

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

But the android:state_pressed= "false" omitted, resulting in the result is no effect.

2, do not write in the state, such as here

android:state_selected="true"

It doesn't work at all, 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 also has the effect.

Android Custom drawable Resource Reference issues

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.