The Android color selector is not available for Android 4
•
Android
I have a defined drawable resource (RES / drawable /. XML), which contains the defined color resource (RES / color /. XML). The color resource contains the state selector
In Android 5, everything works normally and the color status selector works normally. But it doesn't work normally on Android 4. Why does this happen? How to solve it?
Layout:
<Button
android:background="@drawable/btn_login"
drawable / btn_ login.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="@dimen/login_button_border"
android:color="@color/btn_login_border" />
<solid android:color="@color/btn_login_background" />
</shape>
</item>
<item android:bottom="@dimen/login_button_border">
<shape android:shape="rectangle">
<stroke
android:width="@dimen/login_button_border"
android:color="@color/btn_login_background" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
color / btn_ login_ border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/login_border_highlighted" android:state_pressed="true"/>
<item android:color="@color/login_border"/>
</selector>
color / btn_ login_ background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/login_button_highlighted" android:state_pressed="true"/>
<item android:color="@color/login_button"/>
</selector>
resolvent:
Since colorstatelist is not supported in previous versions of lollipop, you can reorganize the displayed resources like this:
drawable / btn_ login.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/login_button_highlighted" android:state_pressed="true"/>
<item android:drawable="@drawable/login_button"/>
</selector>
drawable / btn_ login_ normal.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="@dimen/login_button_border"
android:color="@color/login_border" />
<solid android:color="@color/login_button" />
</shape>
</item>
<item android:bottom="@dimen/login_button_border">
<shape android:shape="rectangle">
<stroke
android:width="@dimen/login_button_border"
android:color="@color/login_button" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
drawable / btn_ login_ highlihted.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="@dimen/login_button_border"
android:color="@color/login_border_highlighted" />
<solid android:color="@color/login_button_highlighted" />
</shape>
</item>
<item android:bottom="@dimen/login_button_border">
<shape android:shape="rectangle">
<stroke
android:width="@dimen/login_button_border"
android:color="@color/login_button_highlighted" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
In this case, this will not require the use of a color selector
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码