Android custom rectangles and the use of selector, shape

Source: Internet
Author: User

"Declaration" reproduced please indicate the source, this article from the tip of the blog: http://blog.csdn.net/huntersnail

--Write a blog every day, do a bit of technical accumulation every day!


Android custom rectangles and the use of selector, shape

As a result of the project development needs, although previously used selector, shape but have not been well researched, only know how to use, do not know their properties specifically what the role. Although on-line a check on all know, feel still want to find out for themselves.

Let's look at the following together:


One, XML layout file

/Test Demo/res/layout/check_box.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <linearlayout android:id= "@+id/order_meth" style= "@style/style_padding" Android:layout_wid Th= "Match_parent" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" Andro id:background= "@drawable/selector_mine" android:orientation= "horizontal" > <textview Androi D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "delivery Mode" an Droid:textsize= "18sp"/> <radiogroup android:id= "@+id/type" android:layout_width= "match _parent "android:layout_height=" wrap_content "android:gravity=" center "android:orientatio n= "Horizontal" > <RadioButton android:id= "@+id/rb_one" android:layout_width= "Wrap_content" Android                oid:layout_height= "Wrap_content" style= "@style/sku_rd" android:layout_marginleft= "20DP"                android:gravity= "Center" android:checked= "true" android:padding= "10DP" Android:text= "one hour Up"/> <radiobutton android:id= "@+id/rb_two" android:l                Ayout_width= "Wrap_content" android:layout_height= "wrap_content" style= "@style/sku_rd"                android:layout_marginleft= "10DP" android:gravity= "center" android:padding= "10DP" android:text= "scheduled delivery"/> </RadioGroup> </LinearLayout></LinearLayout>


Second, the background color processing when clicked

1. Style styles

<resources xmlns:android= "Http://schemas.android.com/apk/res/android" >    <style name= "Sku_rd" >        <item name= "Android:textcolor" > @color/selector_text</item>        <item name= "Android: Background "> @drawable/check_boxs</item>        <item name=" Android:button "> @null </item><!- -Remove the RadioButton Circle-    </style></resources>


2, click the RadioButton Border background color transformation of the key XML

Attribute interpretation

"True" indicates the use of a pressed state (for example, a button is pressed); "false" indicates a non-pressed state.
Android:state_focused= "True/false"

"True" indicates that the focus state is used (for example, using a rolling ball/d-pad focus button); "False" indicates the use of a non-focused state.
Android:state_selected= "True/false"

"True" indicates that the selected state is used (for example, tab-Open); "false" indicates a unchecked state.
Android:state_active= "True/false"

"True" indicates that the status can be checked and "false" means that it is not available for tick status. (Useful only for components that can toggle tick-not tick.) )
Android:state_checkable= "True/false"

"True" indicates that the check status is used; "false" indicates a non-tick status.
Android:state_checked= "True/false"

True "indicates that the check status is used;" false "indicates non-tick status.
Android:state_enabled= "True/false"

"True" indicates that the available state is used (can receive touch/click events); "False" indicates an unavailable state to use.
Android:state_window_focused= "True/false"

"True" indicates that the application window has focus when used (the application is in the foreground); "false" means use when there is no focus (for example, Notification Bar drop or dialog box display).


/Test Demo/res/drawable/check_boxs.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_checked=" true "android:drawable=" @drawable/shape_sku "></item>    <item android:state_pressed= "true" android:drawable= "@drawable/shape_sku" ></item>    <item Android:state_selected= "true" android:drawable= "@drawable/shape_sku" ></item>    <item android: drawable= "@drawable/shape_sku_normal" ></item></selector>


3. Change of text color

/Test Demo/res/color/selector_text.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android >    <!--text When selected background--    <item android:state_focused= "true" android:color= "#DB251F"/>    <!--text Click Background--    <item android:state_pressed= "true" android:color= "#DB251F"/>    <item Android:state_checked= "true" android:color= "#DB251F"/>    <item android:state_selected= "true" Android: Color= "#DB251F"/>    <!--text default background--    <item android:color= "#969696"/></selector>


Three, the following is a custom two rectangle

Attribute interpretation

Corners: Rounded Corners

The Android:radius is the radian of the angle, and the larger the value the greater the rounded.
We can also set the four corners to different angles, while setting five properties, the Radius property is invalid
android:radius= "20DP" sets the radius of Four Corners
android:topleftradius= "20DP" sets the radius of the upper-left corner
Android:toprightradius= "20DP" set the radius of the upper-right corner
android:bottomleftradius= "20DP" setting the radius of the lower right corner
Android:bottomrightradius= "20DP" set the radius of the lower left corner


Stroke: Stroke
Android:width= the width of the "2DP" stroke

The color of the android:color stroke.
We can also make the stroke into a dashed form, set in the following way:
Android:dashwidth= "5DP"
android:dashgap= "3DP"
Where android:dashwidth represents the width of a horizontal line such as '-', android:dashgap represents the distance between them.


Solid: Fill
ANDROID:COLOR Specifies the color of the fill


/Test Demo/res/drawable/shape_sku_normal.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >    <stroke android:width= "1DP" android:color= "#C8C8C8"/>    <corners android:radius= "3DP"/>    <solid android:color= "@android: Color/white"/></shape>
/Test Demo/res/drawable/shape_sku.xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >    <stroke android:width= "1DP" android:color= "#DB251F"/>    <corners android:radius= "3DP"/>    <solid android:color= "@android: Color/white"/></shape>


Four,


Five, the source address Click to open link


☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ Reprint Please indicate the source? A blog ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ with your fingertips

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android custom rectangles and the use of selector, shape

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.