shape 屬性介紹
可以通過設(shè)置 angle 來自定義漸變方向吭敢,它是從左向右碰凶、逆時針轉(zhuǎn)的。
當(dāng) angle 設(shè)置為0度時 :
當(dāng)angle設(shè)置為90度時:
注意:設(shè)置的值以45度為一個單位鹿驼。
radial樣式的效果圖:
資源文件
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jkxy.custombutton.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="50sp"
android:text="Click me"
android:textAllCaps="false"
android:background="@drawable/button_enabled" />
<Button
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="50sp"
android:text="Click me"
android:textAllCaps="false"
android:background="@drawable/button_disabled" />
</LinearLayout>
第一個可點(diǎn)擊的Button:
drawable/button_enabled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--正常狀態(tài)時-->
<item android:state_pressed="false" android:drawable="@drawable/button_enabled_normal" />
<!--被按下狀態(tài)時-->
<item android:state_pressed="true" android:drawable="@drawable/button_enabled_pressed" />
</selector>
drawable/button_enabled_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="25dp">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#B42DA0"
android:endColor="#B12598"/>
<!--設(shè)置上面兩個角的弧度-->
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
</item>
<item android:top="50dp">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#94006C"
android:endColor="#C20894"/>
<!--設(shè)置下面兩個角的弧度-->
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
</shape>
</item>
</layer-list>
drawable/button_enabled_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="25dp">
<shape android:shape="rectangle">
<!--設(shè)置漸變色-->
<gradient
android:angle="270"
android:startColor="#F1983C"
android:endColor="#F49C2D"/>
<!--設(shè)置上面兩個角的弧度-->
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
</item>
<item android:top="50dp">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#ED6B01"
android:endColor="#F47307"/>
<!--設(shè)置下面兩個角的弧度-->
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
</shape>
</item>
</layer-list>
第二個不可點(diǎn)擊的Button:
drawable/button_disabled.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!--漸變1-->
<item
android:top="1dp"
android:left="1dp"
android:right="1dp"
android:bottom="25dp">
<shape android:shape="rectangle" >
<!--設(shè)置漸變色-->
<gradient
android:angle="90"
android:startColor="#F88800"
android:endColor="#CB5500" />
<!--設(shè)置角的弧度-->
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
</item>
<!--漸變2-->
<item
android:top="50dp"
android:left="1dp"
android:right="1dp"
android:bottom="1dp" >
<shape android:shape="rectangle" >
<gradient
android:startColor="#FDD401"
android:endColor="#E68E15"
android:type="radial"
android:centerY="0"
android:gradientRadius="250" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp" />
</shape>
</item>
<!--設(shè)置邊框-->
<item>
<shape android:shape="rectangle" >
<corners android:radius="8dp" />
<stroke
android:width="2dp"
android:color="#862501" />
</shape>
</item>
</layer-list>