先在 res/drawable 下設定一個 bg_round_button.xml 背景定義檔:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp" /> <solid android:color="#3D998D" /> </shape>
然後在佈局 Layout 的按鈕中,將 background 設定為 @drawable/bg_round_button (即背景定義檔的路徑和名稱) 。
<ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_round_button" android:contentDescription="Round Button" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/home" />
然後除了在 Button 、 ImageButton 之外,一般 View 之類可以設定 background 屬性的地方,也可以套用背景定義檔。
PS. 若是正方形的元件,只要 radius 的值等於長或寬的一半的話,就會變成圓形。