步驟:
1.在drawable 文件夾中創(chuàng)建一個(gè)xml布局文件。
2.修改布局文件
3.在需要使用背景的按鍵中導(dǎo)入布局。
創(chuàng)建布局文件:

修改布局文件:
<?xml version="1.0" encoding="utf-8"?> <!--shape:形狀 rectangle 長(zhǎng)方形--> <!--corners 邊角--> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--設(shè)置背景顏色--> <solid android:color="@color/colorblue"/> <!--設(shè)置角為半圓 radius 半徑--> <corners android:radius="10dp"/> <!--bottom 下 top 上 left左 right右--> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape>
預(yù)覽效果圖:

在按鍵中添加布局:

實(shí)際效果:

PS(橢圓按鍵的xml):
<?xml version="1.0" encoding="utf-8"?> <!-- shape 形狀 oval橢圓形--> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!--設(shè)置背景顏色--> <solid android:color="@color/colorgreen"/> <!--設(shè)置高度和寬度--> <size android:height="30dp" android:width="120dp"/> <padding android:bottom="10dp" android:top="10dp" android:left="10dp" android:right="10dp"/> </shape>
總結(jié)
以上所述是小編給大家介紹的android自定義形狀的按鍵實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選