shape 属性介绍
可以通过设置 angle 来自定义渐变方向,它是从左向右、逆时针转的。
当 angle 设置为0度时 :
当angle设置为90度时:
注意:设置的值以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>
第一个可点击的Button:
drawable/button_enabled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--正常状态时-->
<item android:state_pressed="false" android:drawable="@drawable/button_enabled_normal" />
<!--被按下状态时-->
<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"/>
<!--设置上面两个角的弧度-->
<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"/>
<!--设置下面两个角的弧度-->
<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">
<!--设置渐变色-->
<gradient
android:angle="270"
android:startColor="#F1983C"
android:endColor="#F49C2D"/>
<!--设置上面两个角的弧度-->
<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"/>
<!--设置下面两个角的弧度-->
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
</shape>
</item>
</layer-list>
第二个不可点击的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" >
<!--设置渐变色-->
<gradient
android:angle="90"
android:startColor="#F88800"
android:endColor="#CB5500" />
<!--设置角的弧度-->
<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>
<!--设置边框-->
<item>
<shape android:shape="rectangle" >
<corners android:radius="8dp" />
<stroke
android:width="2dp"
android:color="#862501" />
</shape>
</item>
</layer-list>