ボタンに境界線を追加するにはどうすればよいですか? 画像を使用せずにこれを行うことは可能ですか?
11 に答える
ステップ 1: my_button_bg.xml という名前のファイルを作成します。
ステップ 2 : このファイルを res/drawables.xml に配置します。
ステップ 3 : 以下のコードを挿入
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#00FF00"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
ステップ 4: コード "android:background="@drawable/my_button_bg" を必要に応じて使用します。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:background="@drawable/my_button_bg"
/>
button_border.xml
drawable フォルダーにファイルを作成します。
res/drawable/button_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFDA8200" />
<stroke
android:width="3dp"
android:color="#FFFF4917" />
</shape>
XML アクティビティ レイアウトにボタンを追加し、背景を設定しますandroid:background="@drawable/button_border"
。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_border"
android:text="Button Border" />
シェイプ ドローアブルの作成についてはこちらをご覧ください http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
これが完了したら、ボタン セットの XML でandroid:background="@drawable/your_button_border"
マテリアル コンポーネント ライブラリではMaterialButton
、Widget.MaterialComponents.Button.OutlinedButton
スタイルでa を使用するだけです。
<com.google.android.material.button.MaterialButton
....
style="?attr/materialButtonOutlinedStyle"
app:strokeColor="@color/colorPrimary"/>
Jetpack 構成では、次を使用しますOutlinedButton
。
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
) {
Text(text = "BORDER")
}