Android Custom button button display style

Source: Internet
Author: User

About the ListView and button to change the background of the original Android control, on the Internet to find some information is not very full, so now summarize the use of Android selector.

First of all, Android selector is configured in Drawable/xxx.xml.

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.

But this will appear the list 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>

Finally, you need to add two items to the XML file that contains the button. If it is main.xml file, we need to add two items in <button/>.

Android:focusable= "true"

android:backgroud= "@drawable/button_color"

This way, when you use the button, you can get rid of the yellow color background of the system, realize the personalized background, with the overall layout of the application is very useful AH

Android Custom button button display style

Related Article

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.