Android Shape graphics optimize button effect

Source: Internet
Author: User

Android shape allows us to create graphics by defining an XML file, but of course we can only implement some simple graphics (round, rectangle, ellipse, line segment), but it's pretty good, the shape created by shapes as the background of the control has basically been able to meet my simple needs, And the shapes created by shape can be adapted to a variety of screens.

The following is a graph defined by shape to optimize the overall effect of the button.

Define the main layout file Activity_main.xml:

1<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"2xmlns:tools="Http://schemas.android.com/tools"3Android:layout_width="match_parent"4android:layout_height="match_parent"5tools:context=". Mainactivity">6 7<Button8android:layout_height="48DP"9Android:layout_width="160DP"TenAndroid:background="@drawable/button_selector" OneAndroid:layout_centerinparent="true" Aandroid:text="Button"/> -</RelativeLayout>

Defines the state selector for the button button_selector.xml:

1<?xml version="1.0"encoding="Utf-8"?>2<selector3Xmlns:android="http://schemas.android.com/apk/res/android">4<item android:state_focused="false"Android:state_selected="true"Android:state_pressed="false"android:drawable="@drawable/button_shape"/>5<item android:state_focused="false"Android:state_selected="false"Android:state_pressed="false"android:drawable="@drawable/button_shape"/>6<item android:state_pressed="true"android:drawable="@drawable/button_shape_press"/>7<item android:state_focused="true"Android:state_selected="true"Android:state_pressed="false"android:drawable="@drawable/button_shape"/>8</selector>

Defines the background button_shape.xml of the button's non-clicked state:

1<?xml version="1.0"encoding="UTF-8"Standalone="No"?>2<shape xmlns:android="http://schemas.android.com/apk/res/android"3Android:shape="Rectangle">4<!--gradients--5<gradient android:angle="0"Android:endcolor="#DCDCE1"Android:startcolor="#DCDCE1"Android:centercolor="#DCDCE1"/>6<!--solid--7<solid android:color="#DCDCE1"/>8<!--rounded corners to define the size of four rounded corners-9<corners android:radius="3DP"/>Ten<!--strokes to set the width of the edge color-- One<stroke android:width="1DP"Android:color="#C8C8CD"/> A<!--padding-- -<padding android:bottom="0DP"android:left="0DP"android:right="0DP"android:top="0DP"/> -</shape>

Defines the background button_shape_press.xml of the button click state:

1<?xml version="1.0"encoding="UTF-8"Standalone="No"?>2<shape xmlns:android="http://schemas.android.com/apk/res/android"3Android:shape="Rectangle">4<gradient android:angle="0"Android:endcolor="#C8C8CD"Android:startcolor="#C8C8CD"Android:centercolor="#C8C8CD"/>5<solid android:color="#C8C8CD"/>6<corners android:radius="3DP"/>7<stroke android:width="1DP"Android:color="#C8C8CD"/>8<padding android:bottom="0DP"android:left="0DP"android:right="0DP"android:top="0DP"/>9</shape>

The final effect is as follows:

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.