步驟:
1.在drawable 檔案夾中建立一個xml布局檔案。
2.修改布局檔案
3.在需要使用背景的按鍵中導入布局。
建立布局檔案:
修改布局檔案:
<?xml version="1.0" encoding="utf-8"?>
<!--shape:形狀 rectangle 長方形-->
<!--corners 邊角-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--設定背景顔色-->
<solid android:color="@color/colorblue"/>
<!--設定角為半圓 radius 半徑-->
<corners android:radius="10dp"/>
<!--bottom 下 top 上 left左 right右-->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
</shape>
預覽效果圖:
在按鍵中添加布局:
實際效果:
PS(橢圓按鍵的xml):
<?xml version="1.0" encoding="utf-8"?>
<!-- shape 形狀 oval橢圓形-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!--設定背景顔色-->
<solid android:color="@color/colorgreen"/>
<!--設定高度和寬度-->
<size android:height="30dp"
android:width="120dp"/>
<padding
android:bottom="10dp"
android:top="10dp"
android:left="10dp"
android:right="10dp"/>
</shape>