•效果展示圖
•實現方法
點擊 app/src/main/res 找到 drawable 文件夾,右擊->New->Drawable Resource File。
創建一個 $drawable\ resource\ file$,命名為 $shape\_circle$;
在 $shape.xml$ 文件中添加如下代碼:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!-- 填充顏色 --> <solid android:color="#30CCF3" /> <!-- 設置按鈕的四個角為弧形 --> <!-- android:radius 弧形的半徑 --> <corners android:radius="360dip" /> <!-- padding: Button 里面的文字與Button邊界的間隔 --> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape>然后,在 $<Button>$ 中調用 $shape\_circle$ 即可;
需要注意的是,要想實現圓形效果,需要把該 $<Button>$ 組件的長寬比設置為 1:1;
代碼如下:
<Button android:background="@drawable/btn_shape_circle" android:layout_centerInParent="true" android:layout_height="150dp" android:layout_width="150dp" android:text="我是圓形按鈕" />