角を丸くしたいbutton
。Androidでこれを達成する簡単な方法はありますか?
17 に答える
以下のような drawable フォルダーに xml ファイルを作成します。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners android:radius="10dp"/>
</shape>
これを角を丸くしたいボタンの背景として適用します。
または、以下のようにコーナーごとに個別の半径を使用できます
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
Androidでこれを達成する簡単な方法はありますか?
はい、今日あります。とても簡単です。Material Components ライブラリの を属性とともに
使用するだけMaterialButton
です。app:cornerRadius
何かのようなもの:
<com.google.android.material.button.MaterialButton
android:text="BUTTON"
app:cornerRadius="8dp"
../>
角の丸いボタンを取得するだけで十分です。
マテリアル ボタンスタイルの 1 つを使用できます。例えば:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
.../>
また、バージョン1.1.0から、ボタンの形状を変更することもできます。shapeAppearanceOverlay
ボタンスタイルで属性を使用するだけです:
<style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Button.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">16dp</item>
</style>
次に、次を使用します。
<com.google.android.material.button.MaterialButton
style="@style/MyButtonStyle"
.../>
shapeAppearanceOverlay
xml レイアウトに適用することもできます。
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
.../>
shapeAppearance
では、コーナーごとに異なる形状と寸法を設定することもできます。
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopLeft">32dp</item>
<item name="cornerSizeBottomLeft">32dp</item>
</style>
Jetpack Composeでは、次のshape
パラメーターを使用できます。
Button( onClick = { /* Do something! */ },
shape = RoundedCornerShape(8.dp)) {
Text("Button")
}
ファイル myButton.xml を作成します
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorButton"/>
<corners android:radius="10dp"/>
</shape>
ボタンに追加
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/myButton"/>
私が見つけた簡単な方法は、ドローアブルフォルダーに新しいxmlファイルを作成し、ボタンの背景をそのxmlファイルに向けることでした。使用したコードは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>
</shape>
Drawable フォルダーに rounded_btn.xml ファイルを作成します...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/#FFFFFF"/>
<stroke android:width="1dp"
android:color="@color/#000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="5dip" android:bottomLeftRadius="5dip"
android:topLeftRadius="5dip" android:topRightRadius="5dip"/>
</shape>
ボタンの背景として this.xml ファイルを使用します
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_btn"
android:text="Test" />
ベクター ドローアブルを使用している場合は、ドローアブル定義で <corners> 要素を指定するだけです。これについてはブログ記事で取り上げました。
ビットマップ / 9 パッチ ドローアブルを使用している場合は、ビットマップ イメージに透明なコーナーを作成する必要があります。
以下のようなカードレイアウトを使用することもできます
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="60dp"
app:cardCornerRadius="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Template"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>