Styles and custom button styles in Android

Source: Internet
Author: User

1) custom button style

First, the use of picture mode

First create a new Android XML file, type select drawable, root node Select selector, customize a file name.

Then, the development environment automatically in the new file added selector node, we only need to write in the selector node three states when the background image (press, get focus, normal). Specific as follows:

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <Itemandroid:state_pressed= "true"android:drawable= "@drawable/play_press" ;/>    <Itemandroid:state_focused= "true"android:drawable= "@drawable/play_press" ;/>    <Itemandroid:drawable= "@drawable/play" ;/></selector>

Note: Here to get the focus and click on the display of the same picture, must be strictly according to the order of the above to write, not pour.
Finally, whenever you write a button control at layout time, apply the Background property to the button, such as:

<android:id= "@+id/button1"  android:layout_width= "Wrap_content"  android:layout_height= "Wrap_content"android:background= "@drawable/button_ Style "></button>

Ii. adoption of a custom approach

In the source code, only the Button_style file needs to be modified, and the same three states are defined separately:

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <Itemandroid:state_pressed= "true">        <Shape>            <GradientAndroid:startcolor= "#0d76e1"Android:endcolor= "#0d76e1"Android:angle= "+" />            <StrokeAndroid:width= "1dip"Android:color= "#f403c9" />            <CornersAndroid:radius= "2DP" />            <paddingAndroid:left= "10DP"Android:top= "10DP"Android:right= "10DP"Android:bottom= "10DP" />        </Shape>    </Item>      <Itemandroid:state_focused= "true">        <Shape>            <GradientAndroid:startcolor= "#ffc2b7"Android:endcolor= "#ffc2b7"Android:angle= "+" />            <StrokeAndroid:width= "1dip"Android:color= "#f403c9" />            <CornersAndroid:radius= "2DP" />            <paddingAndroid:left= "10DP"Android:top= "10DP"Android:right= "10DP"Android:bottom= "10DP" />        </Shape>    </Item>      <Item>        <Shape>            <GradientAndroid:startcolor= "#000000"Android:endcolor= "#ffffff"Android:angle= " the" />            <StrokeAndroid:width= "1dip"Android:color= "#f403c9" />            <CornersAndroid:radius= "5dip" />            <paddingAndroid:left= "10DP"Android:top= "10DP"Android:right= "10DP"Android:bottom= "10DP" />        </Shape>    </Item></selector>

Note: Each attribute in the code means:

Gradient Body Gradient

StartColor start color, EndColor end color,

Angle start gradient angle (values can only be multiples of 90, 0 o'clock left to right gradients, 90 o'clock down to top gradients, counterclockwise and so on)
Stroke border width border, color border
Corners fillet radius radius, 0 is right angle
Padding the relative position of the text value

2) Customize style style

I. Customizing Styles in Style.xml

As an example of customizing text size and color for text, customizing a style code named "Teststyle" is as follows:

<Resourcesxmlns:android= "Http://schemas.android.com/apk/res/android">     <stylename= "Appbasetheme"Parent= "Android:Theme.Light">     </style>     <stylename= "Apptheme"Parent= "Appbasetheme">     </style>      <stylename= "Teststyle"><item name= "android:textsize" >30px</item> <item name= "Android:textcolor" > #1110CC & lt;/item> <item name= "android:width" >150dip</item> <item name= "Android:height" >150dip </item></style> </Resources>  

Second, the style style of the custom "Teststyle" is referenced in the layout file

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >     <TextViewstyle= "@style/teststyle"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"Android:text= "@string/hello_world" /> </Relativelayout>  

From the above code can be seen, the application is @style/teststyle.

Styles and custom button styles in Android

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.