The use of Android selector and shape

Source: Internet
Author: User

Selector:

In the development process, we usually need to add a click to the control effect, the effect is as follows:

Default Effect Click Effect

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6B/E8/wKioL1U6ASTw5-TpAAAJbnPWtTw124.jpg "title=" Start_ Empty.png "alt=" wkiol1u6astw5-tpaaajbnpwttw124.jpg "/> 650" this.width=650; "src=" http://s3.51cto.com/wyfs02/M01 /6b/ec/wkiom1u5_9lq5n-daaasahxgeec467.jpg "title=" Star_full.png "alt=" Wkiom1u5_9lq5n-daaasahxgeec467.jpg "/>


1, first prepare two pictures (default and click on the picture)

2, right click the Res folder, create a new XML file, in the popup dialog box, Resource Type Select drawable,root Element Select Selector, fill in the file name (such as: Pressed_selector), click Done, A drawable folder is generated in the Res folder to hold the XML file we created

3. The XML code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" True "//click State android:drawable=" @drawable/ic_pressed "& gt;</item>//Click the status of the picture <item android:drawable= "@drawable/ic_normal" ></item>//No setting status is the default picture </ Selector>


4, after the layout file, we want to increase the click effect of the control set the following properties, after running the control has a click effect

android:background= "@drawable/pressed_selector"

5, of course, the effect of setting the click, you can also set the selected state, focus state and so on effects

Just set the appropriate Android:state_xxx property to



Shape:

In addition to setting the click Effect on the control, we can also beautify the appearance of the control, such as setting rounded corners, borders, etc., it is time to use shape, like selector to create an XML file, the Root element select shape, will generate a shape XML file, Various properties refer to the following code

<?xml version= "1.0"  encoding= "Utf-8"? ><shape xmlns:android= "http://schemas.android.com /apk/res/android  >    <!--  Set fillet   radius to set 4-direction fillets, or set 4 corners separately--     <corners         android:radius= "2DP"         android:topleftradius= "2DP"          android:toprightradius= "2DP"         android: bottomleftradius= "2DP"         android:bottomrightradius= "2DP"  / >    <!--  Stroke, that is, the border, the default is solid line; Dashwidth, dashgap two properties set dashed lines  -->     <stroke        android:width= "2DP"          android:color= "#dcdcdc"          android: Dashwidth= "2DP"         android:dashgap= "3DP"/>    <!--  solid, that is, the color of the fill   -->    <solid android:color= "#ffffff"  />         <!--  Gradients  -->    <gradient         android:angle= "        android:endcolor=" # 00ff00 "        android:startcolor=" #ffffff " />         <!--  Interval  -->    <padding         android:bottom= "10DP"          android:left= "10DP"         android:right= "10DP"          android:top= "10DP"  /></shape>

Also, set the Background property on the control to introduce the shape effect

android:background= "@drawable/shape"



Selector is used in combination with shape:

If we want to set the click Effect and shape effect for the control at the same time, we just need to port the shape's code to selector, the code is as follows:

<?xml version= "1.0"  encoding= "Utf-8"? ><selector xmlns:android= "http// Schemas.android.com/apk/res/android ">    <!--  set shape --> for click and default status respectively     <item android:state_pressed= "true" >    <shape>             <corners android:radius= "20DP"  />            <solid android: Color= "#e0e0e0"  />        </shape>         </item>             <item>    <shape>             <corners android:radius= "20DP"  />             <solid&nbsP;android:color= "#ffffff"  />        </shape></item ></selector>


The use of Android selector and 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.