The use of Android selector and shape

Source: Internet
Author: User

the use of shape:shape is used to set the shape, can be used in selector,layout, etc., there are 6 sub-tags, the following properties:

Fill: Sets the color of the fill

interval: sets the interval in four directions

Size: set Size

fillet: set five properties at the same time, the RADIUS attribute 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

strokes:dashwidth and Dashgap properties, as long as one is set to 0DP, the border is the implementation border

Android:width= "20DP" sets the width of the edge

Android:color= "@android: Color/black" Sets the color of edges

Android:dashwidth= "2DP" to set the width of the dashed line

android:dashgap= "20DP" sets the interval width of the dashed line

Gradients: when the fill color is set, there is no gradient effect. The value of the angle must be a multiple of 45 (including 0), only valid in type= "linear", or it will be an error. Android:uselevel This attribute does not know what is the use.

The specific wording is as follows

<?XML version= "1.0" encoding= "Utf-8"?><Shapexmlns:android= "Http://schemas.android.com/apk/res/android" >        <!--Rounded Corners -    <CornersAndroid:radius= "9DP"Android:topleftradius= "2DP"Android:toprightradius= "2DP"Android:bottomleftradius= "2DP"Android:bottomrightradius= "2DP"/><!--set the fillet radius -        <!--Gradient -    <GradientAndroid:startcolor= "@android: Color/white"Android:centercolor= "@android: Color/black"Android:endcolor= "@android: Color/black"Android:uselevel= "true"Android:angle= "$"Android:type= "Radial"Android:centerx= "0"Android:centery= "0"Android:gradientradius= "All"/>        <!--interval -    <paddingAndroid:left= "2DP"Android:top= "2DP"Android:right= "2DP"Android:bottom= "2DP"/><!--intervals in each direction -        <!--size -    <sizeAndroid:width= "50DP"Android:height= "50DP"/><!--Width and Height -        <!--Fill -    <SolidAndroid:color= "@android: Color/white"/><!--the color of the fill -        <!--Strokes -    <StrokeAndroid:width= "2DP"Android:color= "@android: Color/black"Android:dashwidth= "1DP"Android:dashgap= "2DP"/>    </Shape>
View Code

Second, the use of selector

First look at the status in the ListView:
Save the following XML file as your own named. xml file (for example, List_item_bg.xml), using the corresponding background image when the system is used, based on the state of the list item in the ListView.
Drawable/list_item_bg.xml

<?XML version= "1.0" encoding= "Utf-8"?>    <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">        <!--background picture by default -        <Itemandroid:drawable= "@drawable/pic1" />        <!--background picture When there is no focus -        <Itemandroid:state_window_focused= "false"android:drawable= "@drawable/pic1" />        <!--background picture When you get focus in non-touch mode and click -        <Itemandroid:state_focused= "true"android:state_pressed= "true"android:drawable= "@drawable/pic2" />        <!--background picture when clicked in touch mode -        <Itemandroid:state_focused= "false"android:state_pressed= "true"android:drawable= "@drawable/pic3" />        <!--picture Background When selected -        <Itemandroid:state_selected= "true"android:drawable= "@drawable/pic4" />        <!--picture background when getting focus -        <Itemandroid:state_focused= "true"android:drawable= "@drawable/pic5" />    </selector>

Use some XML files: The first is to configure android:listselector= "@drawable/LIST_ITEM_BG" in the ListView

Either add the attribute android:background= "@drawable/list_item_bg" to the ListView item or use it in Java code: drawable drawable = Getresources (). getdrawable (R.DRAWABLE.LIST_ITEM_BG);

Listview.setselector (drawable); the same effect.

However, this will appear when the list is sometimes black, need to add: android:cachecolorhint= "@android: color/transparent" make it transparent.

Second, take a look at some of the background effects of button:

Android:state_selected is selected

Android:state_focused is gaining focus

Android:state_pressed is click

Android:state_enabled is the set response event, which refers to all events

The selector effect of the button can also be set according to these states. You can also set selector to change the state of text in a button.

Here's how to configure the text effect in the button:

Drawable/button_font.xml

 <?XML version= "1.0" encoding= "Utf-8"?>    <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">        <Itemandroid:state_selected= "true"Android:color= "#FFF" />        <Itemandroid:state_focused= "true"Android:color= "#FFF" />        <Itemandroid:state_pressed= "true"Android:color= "#FFF" />        <ItemAndroid:color= "#000" />    </selector>

Button can also achieve more complex effects, such as gradients and so on.

Drawable/button_color.xml

<?XML version= "1.0" encoding= "Utf-8"?>    <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">        <Itemandroid:state_pressed= "true">            <!--defines the pattern when the button is in the pressed state.  -            <Shape>                <GradientAndroid:startcolor= "#8600ff" />                <StrokeAndroid:width= "2DP"Android:color= "#000000" />                <CornersAndroid:radius= "5DP" />                <paddingAndroid:left= "10DP"Android:top= "10DP"Android:bottom= "10DP"Android:right= "10DP" />            </Shape>        </Item>        <Itemandroid:state_focused= "true">            <!--define the shape when the button gets focus -            <Shape>                <GradientAndroid:startcolor= "#eac100" />                <StrokeAndroid:width= "2DP"Android:color= "#333333"Color= "#ffffff" />                <CornersAndroid:radius= "8DP" />                <paddingAndroid:left= "10DP"Android:top= "10DP"Android:bottom= "10DP"Android:right= "10DP" />            </Shape>        </Item>    </selector>
View Code

Finally, you need to add a background to the XML file that contains the button. If it is a main.xml file, we need to add an item in <button/>.

android:backgroud= "@drawable/button_color"

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.