android xml實(shí)現(xiàn)按鈕的圓角、陰影效果及按下變化效果的實(shí)現(xiàn)代碼
前言
5個xml文件實(shí)現(xiàn) 按鈕的圓角、陰影效果以及按下變化效果
實(shí)現(xiàn)
drawable/shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 圓角 -->
<corners
android:radius="8dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"/><!-- 設(shè)置圓角半徑 -->
<!-- 間隔 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/><!-- 各方向的間隔 -->
<!-- 大小 -->
<size
android:width="50dp"
android:height="50dp"/><!-- 寬度和高度 -->
<!-- 填充 -->
<solid
android:color=" #FF03A9F4"/><!-- 填充的顏色 -->
</shape>
drawable/shapepress.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 圓角 -->
<corners
android:radius="8dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"/><!-- 設(shè)置圓角半徑 -->
<!-- 間隔 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/><!-- 各方向的間隔 -->
<!-- 大小 -->
<size
android:width="50dp"
android:height="50dp"/><!-- 寬度和高度 -->
<!-- 填充 -->
<solid
android:color="#FF7AD1F8"/><!-- 填充的顏色 -->
</shape>
drawable/shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--第一層陰影-->
<item>
<shape android:shape="rectangle">
<solid android:color="#21000000" />
<corners android:radius="8dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp" />
<!-- 大小 -->
<size
android:width="50dp"
android:height="50dp"/><!-- 寬度和高度 -->
</shape>
</item>
<!--第二層前景-->
<item
android:bottom="2dp"
android:right="3dp"
android:drawable="@drawable/shape"
>
</item>
</layer-list>

drawable/shadowpress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--第一層陰影-->
<item>
<shape android:shape="rectangle">
<solid android:color="#0E000000" />
<corners android:radius="8dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp" />
<!-- 大小 -->
<size
android:width="50dp"
android:height="50dp"/><!-- 寬度和高度 -->
</shape>
</item>
<!--第二層前景-->
<!--第二層前景-->
<item
android:bottom="2dp"
android:right="3dp"
android:drawable="@drawable/shapepress"
>
</item>
</layer-list>

drawable/button_style.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--注意先后順序-->
<!-- 觸摸模式下單擊時的陰影-->
<item android:state_pressed="true" android:drawable="@drawable/shadowpress" />
<!-- 默認(rèn)時的背景圖片-->
<item android:drawable="@drawable/shadow" />
</selector>
activity_main.xml中引入
<Button android:id="@+id/button9" android:onClick="pushNumber" android:text="9" android:background="@drawable/button_style" android:textColor="@color/colorWhite" android:layout_margin="5dp" />

效果動態(tài)圖

以上就是android xml實(shí)現(xiàn)按鈕的圓角、陰影效果以及按下變化效果的詳細(xì)內(nèi)容,更多關(guān)于android按鈕按下變化效果的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android9.0 SystemUI 網(wǎng)絡(luò)信號欄定制修改的流程解析
Android連接服務(wù)器端的Socket的實(shí)例代碼
Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
Android形狀圖形與狀態(tài)列表圖形及九宮格圖片超詳細(xì)講解
如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果
Android仿美團(tuán)分類下拉菜單實(shí)例代碼

