Android set round button with shape

Source: Internet
Author: User

Shape is often used in Android to define some of the display properties of a control to beautify the UI;

The common properties of shape are:

(1) Solid: Fill, set the color of the fill;

(2) Stroke: stroke, set the width of the boundary, color, etc.;

(3) Corners: Rounded corners, five properties, all set, will be overwritten;

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

(4) Padding: Defines the distance of content from the boundary, where the properties are similar to android:padding_left,android:padding_right;

(5) Gradient: corresponding color gradient; When the fill color is set, there is no gradient effect, Android:angle is the angle from which to start, the value of angle must be a multiple of 45 (including 0), only in type= "linear" valid, otherwise will be error;

(6) Size: set size;

For example:

Activity_main.xml

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <TextView android:layout_width="match_parent"Android:layout_height="wrap_content"android:gravity="Center"Android:text="@string/hello_world"android:textsize="30SP"/> <LinearLayout android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Horizontal"> <Button Android:id="@+id/roundbutton"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_marginleft="15DP"Android:background="@layout/shape1"Android:text="@string/button1"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_marginleft="15DP"Android:background="@layout/shape2"Android:text="@string/button1"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_marginleft="15DP"Android:background="@layout/shape3"Android:text="@string/button1"/> </LinearLayout></LinearLayout>

Shape1.xml:

<?xml version="1.0"encoding="UTF-8"? ><shape xmlns:android="http://schemas.android.com/apk/res/android"Android:shape="Rectangle"> <!--filled colors-<solid android:color="#99FFFF"/> <!--set the four corners of the button arc-to-<corners android:radius="20DP"/> <!--padding:button spacing between text and button boundaries--<padding Android:bottom="10DP"Android:left="10DP"Android:right="10DP"Android:top="10DP"/></shape>

Shape2.xml

<?xml version="1.0"encoding="UTF-8"? ><shape xmlns:android="http://schemas.android.com/apk/res/android"Android:shape="Rectangle"> <!--filled colors-<solid android:color="#FFCC66"/> <!--The lower-left and lower-right corner of the settings button is a rounded border--<Corners Android:bottomleftradius="20DP"Android:bottomrightradius="20DP"/> <!--strokes-<Stroke Android:width="1DP"Android:color="#000000"/> <!--padding:button spacing between text and button boundaries--<padding Android:bottom="10DP"Android:left="10DP"Android:right="10DP"Android:top="10DP"/></shape>

Shape3.xml

<?xml version="1.0"encoding="UTF-8"? ><shape xmlns:android="http://schemas.android.com/apk/res/android"Android:shape="Rectangle"> <!--filled colors-<solid android:color="#996600"/> <!--set the upper-left and upper-right corner of the button to a rounded border-<Corners Android:topleftradius="20DP"Android:toprightradius="20DP"/> <!--padding:button spacing between text and button boundaries--<padding Android:bottom="10DP"Android:left="10DP"Android:right="10DP"Android:top="10DP"/></shape>

Mainactivity.java

Package Com.xiaozhang.listview2;import android.app.activity;import android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import android.widget.button;import android.widget.Toast; Public classMainactivity extends Activity {Button Roundbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Roundbutton=(Button) Findviewbyid (R.id.roundbutton); Roundbutton.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {toast.maketext (mainactivity. This,"you clicked the Fillet button", Toast.length_long). Show ();    }        }); }}

Android set round button with 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.