Android Selector and Shape usage, androidselector
I. Shape usage:Shape is used to set the shape. It can be used in selector, layout, and so on. There are 6 sub-labels. Each attribute is as follows:
Fill: Set the fill color
Interval: Set the interval in four directions.
Size: set the size.
Rounded corner: if five attributes are set at the same time, the Radius attribute is invalid.
Android: Radius = "20dp" sets the Radius of the four corners
Android: topLeftRadius = "20dp" sets the radius in the upper left corner.
Android: topRightRadius = "20dp" sets the radius in the upper right corner.
Android: bottomLeftRadius = "20dp" sets the radius in the lower right corner.
Android: bottomRightRadius = "20dp" sets the radius in the lower left corner.
Stroke: The dashWidth and dashGap attributes. If one of them is set to 0dp, the border is the implemented border.
Android: width = "20dp": Set the width of Edge
Android: color = "@ android: color/black" sets the edge color
Android: dashWidth = "2dp" set the width of the dotted line
Android: dashGap = "20dp" sets the interval width of the dotted line.
Gradient: No gradient effect after the fill color is set. The angle value must be a multiple of 45 (including 0) and only valid in type = "linear". Otherwise, an error is returned. Android: useLevel.
The statement is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Rounded corner --> <corners android: radius = "9dp" android: topLeftRadius = "2dp" android: topRightRadius = "2dp" android: bottomLeftRadius = "2dp" android: bottomRightRadius = "2dp"/> <! -- Set the radius of the rounded corner --> <! -- Gradient --> <gradient android: startColor = "@ android: color/white" android: centerColor = "@ android: color/black" android: endColor = "@ android: color/black "android: useLevel =" true "android: angle =" 45 "android: type =" radial "android: centerX =" 0 "android: centerY = "0" android: gradientRadius = "90"/> <! -- Interval --> <padding android: left = "2dp" android: top = "2dp" android: right = "2dp" android: bottom = "2dp"/> <! -- Interval in each direction --> <! -- Size --> <size android: width = "50dp" android: height = "50dp"/> <! -- Width and height --> <! -- Fill --> <solid android: color = "@ android: color/white"/> <! -- Fill color --> <! -- Stroke --> <stroke android: width = "2dp" android: color = "@ android: color/black" android: dashWidth = "1dp" android: dashGap = "2dp"/> </shape>View Code
Ii. Usage of Selector
First look at the status in listview:
Save the following XML file as your own. xml file (for example, list_item_bg.xml). When using the system, you can use the corresponding background image based on the status of the list items in the ListView.
Drawable/list_item_bg.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Selector xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Default background image --> <item android: drawable = "@ drawable/pic1"/> <! -- Background image without focus --> <item android: state_window_focused = "false" android: drawable = "@ drawable/pic1"/> <! -- Background image when the focus is obtained in non-touch mode and clicked --> <item android: state_focused = "true" android: state_pressed = "true" android: drawable = "@ drawable/pic2"/> <! -- Background image when you click in touch mode --> <item android: state_focused = "false" android: state_pressed = "true" android: drawable = "@ drawable/pic3"/> <! -- Background of the selected image --> <item android: state_selected = "true" android: drawable = "@ drawable/pic4"/> <! -- Image background when getting focus --> <item android: state_focused = "true" android: drawable = "@ drawable/pic5"/> </selector>
Use some xml files: first, configure android: listSelector = "@ drawable/list_item_bg" in listview"
You can also add the property android: background = "@ drawable/list_item_bg" to the listview item, or use Drawable drawable = getResources () in java code (). getDrawable (R. drawable. list_item_bg );
ListView. setSelector (drawable); same effect.
However, when the list is sometimes black, you need to add android: cacheColorHint = "@ android: color/transparent" to make it transparent.
Next, let's take a look at some background Effects of the Button:
Android: state_selected is selected
Android: state_focused is the focal point
Android: state_pressed: Click
Android: state_enabled is used to set whether to respond to events. It refers to all events.
You can also set the selector effect of the button Based on these statuses. You can also set selector to change the text status in the button.
Configure the text effect in the button as follows:
Drawable/button_font.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="#FFF" /> <item android:state_focused="true" android:color="#FFF" /> <item android:state_pressed="true" android:color="#FFF" /> <item android:color="#000" /> </selector>
Button can also achieve more complex effects, such as gradient.
Drawable/button_color.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Selector xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: state_pressed = "true"> <! -- Defines the form of a button when it is in the pressed state. --> <Shape> <gradient android: startColor = "# 8600ff"/> <stroke android: width = "2dp" android: color = "#000000"/> <corners android: radius = "5dp"/> <padding android: left = "10dp" android: top = "10dp" android: bottom = "10dp" android: right = "10dp"/> </shape> </item> <item android: state_focused = "true"> <! -- Define the form when the button obtains the focus --> <shape> <gradient android: startColor = "# eac100"/> <stroke android: width = "2dp" android: color = "#333333" color = "# ffffff"/> <corners android: radius = "8dp"/> <padding android: left = "10dp" android: top = "10dp" android: bottom = "10dp" android: right = "10dp"/> </shape> </item> </selector>View Code
Finally, you need to add the background to the xml file containing the button. For example, the main. xml file must be added to <Button/>.
Android: backgroud = "@ drawable/button_color"