Android -- selector for simple use, Android -- selector
Selector --- selector
Selector is often seen in App usage.
For example, if a button looks white or its hidden color, it may be an image.
Click Next to display another color or image.
Shape is used in combination.
Normal
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Rectangle oval line ring --> <shape android: shape = "rectangle" xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Rounded corner of the four corners --> <corners android: radius = "8dp"/> <! -- Padding --> <padding android: bottom = "5dp" android: left = "3dp" android: right = "3dp" android: top = "5dp"/> <! -- Fill color --> <solid android: color = "# 09A3DC"/> <! -- Border color --> <stroke android: color = "#88000000" android: width = "1dp"/> </shape>
Press status
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Rectangle oval line ring --> <shape android: shape = "rectangle" xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Rounded corner of the four corners --> <corners android: radius = "8dp"/> <! -- Padding --> <padding android: bottom = "5dp" android: left = "3dp" android: right = "3dp" android: top = "5dp"/> <! -- Fill color --> <solid android: color = "#0066A0"/> <! -- Border color --> <stroke android: color = "#88000000" android: width = "1dp"/> </shape>
Selector
<? Xml version = "1.0" encoding = "UTF-8"?> <Selector xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Display the shape android: state_pressed = "true" when you press it. There are multiple options for status options. --> <item android: drawable = "@ drawable/shap_btn_press" android: state_pressed = "true"/> <! -- Display the shape at ordinary times --> <item android: drawable = "@ drawable/shap_btn_normal"/> </selector>
Reference in Layout
<Button android: layout_margin = "10dp" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textColor = "# ffffff" android: background ="@ Drawable/selector_main_btn"Android: text = "OK"/>
If there is an image, you need to create a selector to specify different images in drawable. In ImageView, specify background to use selector, and then specify the phase event to trigger,
Below are click events
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/ic_menu_add_pressed"/> <item android:drawable="@drawable/ic_menu_add_normal"/></selector>