Android Custom Styles Daquan: shape,selector,layer-list,drawable, animation, style

Source: Internet
Author: User

Original: http://keeganlee.me/post/android/20150830The following extracts some of the content: shape

Typically, the XML file defined by shape is stored in the drawable directory, and if the project does not have that directory, create a new one instead of placing it in a directory such as DRAWABLE-HDPI. Only the corresponding control settings (Bg_rectangle_with_stroke_dash.xml) are required

android:background="@drawable/bg_rectangle_with_stroke_dash"
Four types of

You can use shape to customize the shape, and you can define the following four types of shapes, specified by the Android:shape property:

    • Rectangle: Rectangle, default shape, can draw right-angle rectangle, rounded rectangle, arc, etc.
    • Oval: Ellipse, used more is to draw a circle
    • Line: Linear, can draw solid lines and dashed lines
    • Ring: Annular, can draw loop progress bar
Selector

Sometimes, the change in different states is not only the background, pictures, etc., the text color will also change accordingly. And to deal with these different states to show what the problem, it is necessary to use selector to achieve. A <item/> wrap a state and corresponding effect

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <!--when the current window loses focus -    <Itemandroid:drawable= "@drawable/bg_btn_lost_window_focused"android:state_window_focused= "false" />    <!--unavailable -    <Itemandroid:drawable= "@drawable/bg_btn_disable"android:state_enabled= "false" />    <!--when pressed -    <Itemandroid:drawable= "@drawable/bg_btn_pressed"android:state_pressed= "true" />    <!--When selected -    <Itemandroid:drawable= "@drawable/bg_btn_selected"android:state_selected= "true" />    <!--when it is activated -    <Itemandroid:drawable= "@drawable/bg_btn_activated"android:state_activated= "true" />    <!--Default when -    <Itemandroid:drawable= "@drawable/bg_btn_normal" /></selector>

               
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <!--when the current window loses focus -    <ItemAndroid:color= "@android: Color/black"android:state_window_focused= "false" />    <!--unavailable -    <ItemAndroid:color= "@android: Color/background_light"android:state_enabled= "false" />    <!--when pressed -    <ItemAndroid:color= "@android: Color/holo_blue_light"android:state_pressed= "true" />    <!--When selected -    <ItemAndroid:color= "@android: Color/holo_green_dark"android:state_selected= "true" />    <!--when it is activated -    <ItemAndroid:color= "@android: Color/holo_green_light"android:state_activated= "true" />    <!--Default when -    <ItemAndroid:color= "@android: Color/white" /></selector>

  &NBSP;              
Layer-list

Using Layer-list, you can cascade multiple drawable together in order, like tab in a red layer and a white cascade to display the results together.

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <!--First Load Mode -    <!--<item android:drawable= "@drawable/bg_tab_selected" android:state_checked= "true"/> -    <!--second mode of loading -    <Itemandroid:state_checked= "true">        <layer-list>            <!--Red Background -            <Item>                <ColorAndroid:color= "#E4007F" />            </Item>            <!--white background, the farther away from the bottom of the white background, the more red part is leaking out -            <!--The effect is to see the thick red line -            <ItemAndroid:bottom= "4DP"android:drawable= "@android: Color/white" />        </layer-list>    </Item>    <Item>        <layer-list>            <!--Red Background -            <Item>                <ColorAndroid:color= "#E4007F" />            </Item>            <!--White background -            <ItemAndroid:bottom= "1DP"android:drawable= "@android: Color/white" />        </layer-list>    </Item></selector>

Property
    • Solid: Sets the color of the shape fill
    • padding: Inner spacing of content and shape boundaries
    • Gradient: Sets the gradient color of a shape, which can be a linear gradient, a radiation gradient, a sweep gradient
    • Corners: Set rounded corners for rectangle type only
    • Stroke: Set strokes to stroke narimi lines or dashed lines
    • Size: Set the shape's default sizes to set width and height
Animation
    • The XML file for the view animation is placed in the res/anim/directory, and is referenced as R.anim.filename.
    • The XML file for the <animator> property animation is placed in the res/animator/directory. R.animator.filename with reference
Style
    • The Android style is generally defined in the Res/values/styles.xml file, where there is a root element <resource>, and each style definition is defined by a sub-label under <resource> <style > To complete the,<style> by adding multiple <item> to set different properties of the style.
    • In addition, styles can be inherited through the parent property of the <style> tag that declares the style to inherit, 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.
    • The use of styles is not only used for individual view, but also for activity or the entire application, which requires Android to be set in the corresponding <activity> tag or <application> tag: The theme property, in fact, is a style, but is generally referred to as a theme.

Android Custom Styles Daquan: shape,selector,layer-list,drawable, animation, style

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.