1. The Android style is generally defined in the res/values/styles.xml file, which has a root element <resource>, and each style definition is defined by the < Resource> Sub-tab <style> to complete,<style> to set different properties by adding multiple <item> .
Styles can be inherited by declaring a style to inherit through the parent property of the <style> tag, or by a dot prefix (.), preceded by a parent style name, followed by a child style name. The point prefix method only applies to custom styles, and to inherit the Android built-in style, it can only be declared through the parent property.
<!--Inherit Apptheme - <stylename= "Styletest"Parent= "Apptheme"> <Itemname= "Android:textcolor">#ff3300</Item> </style> <!--Inherit Styletest - <stylename= "Styletest.styletheme" > <Itemname= "Android:textsize">30sp</Item> </style>
You can call style= "@style/styletest" in a specific UI component, or you can use the style as a theme for androidmanifest.xml activity or the entire application setting Android:theme = "@style/styletest"
2. Shape
For various rectangles, circles, ellipses, lines, etc. can be drawn through the shape label, the XML file defined by shape is stored in the drawable directory.
Shape can customize shapes by defining the following four types of shapes, specified by the Android:shape property:
- Rectangle: Rectangle, default shape
- Oval: Oval
- Line: Linear, can draw solid lines and dashed lines
- Ring: Annular, can draw loop progress bar
Control fill color, margins, fillets, strokes, and more through the sub-option elements inside the Shap.
A. Rectangle
Solid: Sets the color of the shape fill
padding: Setting the inner space between content and shape boundaries
gradient: Set the gradient color of a shape
Android:type Types of gradients
- linear linear gradient, default gradient type
- radial radiate gradient, when setting this item, Android:gradientradius must also set the
- Sweep Scanning gradients
- Android:startcolor The color at which the gradient starts
- android:endcolor Color of gradient end
- Android:centercolor The color of the middle of the gradient
- android:angle Gradient angle, linear gradient only valid, must be a multiple of 45, 0 means left to right, 90 means bottom to top
- Android:centerx The relative x-coordinate of the center of the gradient, which is only valid when the gradient is emitted, between 0.0 and 1.0, and the default is 0.5, which means that it is in the middle
- Android:centery The relative x-coordinate of the center of the gradient, which is only valid when the gradient is emitted, between 0.0 and 1.0, and the default is 0.5, which means that it is in the middle
- Android:gradientradius The radius of the gradient, only used when the gradient type is radial
- Android:uselevel If True, you can use it in levellistdrawable
Corners: Set the fillet, only for the rectangle type, can be set four angles of different radii of the fillet, when the radius is large, such as 200DP, can be turned into a curved edge
Stroke: Sets the stroke to stroke narimi or dashed lines.
- android:color Color of strokes
- android:width width of stroke
- android:dashwidth The horizontal line length when the dash is set
- Android:dashgap to set the distance between dashes when dashed
B.oval
To draw an ellipse.
Size: Set the shape's default sizes to set width and height
- android:width Width
- android:height Height
C. Line
When drawing a line, there are a few features that you must know:
- Only draw horizontal line, can not draw vertical line;
- The height of the line is set by the Android:width property of the stroke;
- The Android:height property of size defines the height of the entire shape area;
- The height of size must be greater than the width of the stroke, otherwise the line cannot be displayed;
- The line is centered in the entire shape area;
- The left and right side of the line will leave blank spacing, the thicker the line, the larger the blank;
- The view that references the dashed line needs to add the attribute Android:layertype, the value is set to "software", otherwise the dashed line is not displayed.
D. Ring
Some properties of the shape root element apply only to the ring type:
- Radius of inner ring of Android:innerradius
- android:innerradiusratio floating Point, which represents the radius of the inner ring at the width ratio of the ring, the default is 3, which means that the inner ring radius is the width of the ring divided by 3, and the value is overwritten by Android:innerradius
- Thickness of android:thickness ring
- android:thicknessratio floating point, the width ratio of the ring to represent the thickness of the ring, the default is 9, indicating that the ring thickness is the width of the ring divided by 9, the value will be android:thickness overwritten
- Android:uselevel is generally false, otherwise the ring cannot be displayed and is set to true only when used as levellistdrawable
<!--Rectangle -<Shapexmlns:android= "Http://schemas.android.com/apk/res/android"> <SolidAndroid:color= "#e1bee7"></Solid> <paddingAndroid:bottom= "5DP"Android:left= "5DP"Android:right= "5DP"Android:top= "5DP"></padding> <CornersAndroid:radius= "8DP"></Corners></Shape>
3. Selector
For different states, such as Button press, checkbox selection, etc. need different style processing, can be set by Selector's State_xxx property.
Selector tags, you can add one or more item sub-labels, and the corresponding status is defined in the item tag. The XML file that you define can be used as two resources: drawable and color. When used as a drawable resource, typically placed in the drawable directory as shape, item must specify the android:drawable property, and when used as a color resource, it is placed in the color directory, item must specify the Android:color property.
State:
- android:state_enabled: Sets whether touch or click events are available, typically set only when false to indicate an unavailable state
- android:state_pressed: Sets whether to press the state, usually sets the property when True, indicates the pressed state, default to False
- android:state_selected: Sets whether the state is selected, True indicates checked, false indicates unchecked
- android:state_checked: Set check status, mainly used for checkbox and radiobutton,true indication has been checked, false means not checked
- android:state_checkable: Set tick if available status, similar to state_enabled, just state_enabled will affect touch or click events, and state_checkable affect tick events
- android:state_focused: Sets whether to get the focus state, true to get focus, default to False to indicate no focus
- android:state_window_focused: Sets whether the current window receives the focus state, True indicates the focus, false indicates that there is no focus, such as pulling down the notification bar or Popup dialog box, the current interface loses focus; The ListItem of the ListView also triggers the true state when it gets focus, which can be understood as the current window is the ListItem itself
- android:state_activated: The setting is activated, True indicates that it is active, false means inactive, API level 11 and above are supported, and the control's setactivated can be called through code ( Boolean) method to set whether the control is activated
- android:state_hovered: Sets whether the mouse is sliding above the state, true indicates that the mouse is sliding above, the default is False,api level 14 and above to support
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--Radio The style when selected - <Itemandroid:state_checked= "true"> <layer-list> <!--Red Background - <Item> <ColorAndroid:color= "#E4007F"></Color> </Item> <!--White background - <ItemAndroid:bottom= "3DP"android:drawable= "@android: Color/white"></Item> </layer-list> </Item> <!--the default style - <Item> <layer-list> <!--Red Background - <Item> <ColorAndroid:color= "#E4007F" /> </Item> <ItemAndroid:bottom= "1DP"android:drawable= "@android: Color/white"></Item> </layer-list> </Item></selector>
4.layer-list
Layer-list can cascade multiple drawable in the order they are displayed. See selector code.
Reference:
http://keeganlee.me/post/android/20150830
http://keeganlee.me/post/android/20150905
http://keeganlee.me/post/android/20150909
http://keeganlee.me/post/android/20151031
Android Style Development