溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android Button圓角邊框設(shè)計

發(fā)布時間:2024-08-31 12:37:32 來源:億速云 閱讀:85 作者:小樊 欄目:移動開發(fā)

在Android中,為按鈕(Button)設(shè)計圓角邊框,你可以使用以下幾種方法:

方法一:使用XML繪制背景

  1. res/drawable目錄下創(chuàng)建一個新的XML文件,例如rounded_button.xml。
  2. 在該文件中,使用<shape>標(biāo)簽定義一個圓角矩形:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/holo_blue_bright"/> <!-- 按鈕的背景顏色 -->
    <corners android:radius="10dp"/> <!-- 圓角的半徑 -->
    <stroke
        android:width="2dp" <!-- 邊框的寬度 -->
        android:color="@android:color/white"/> <!-- 邊框的顏色 -->
</shape>
  1. 在按鈕的XML布局文件中,將android:background屬性設(shè)置為剛剛創(chuàng)建的背景文件:
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:background="@drawable/rounded_button"/>

方法二:使用預(yù)定義的背景資源

Android Material Design庫提供了一些預(yù)定義的背景資源,你可以直接使用它們?yōu)榘粹o添加圓角邊框。例如,Widget.MaterialComponents.Button.Outlined

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:background="?attr/material_on_primary_emphasis_high_type"/>

方法三:在代碼中設(shè)置背景

你也可以在Java或Kotlin代碼中為按鈕動態(tài)設(shè)置背景。例如,在Java中:

Button button = findViewById(R.id.button);
ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundedRectShape(10, 10, 10, 10)); // 創(chuàng)建圓角矩形
shapeDrawable.getPaint().setColor(Color.WHITE); // 設(shè)置邊框顏色
shapeDrawable.getPaint().setStrokeWidth(2); // 設(shè)置邊框?qū)挾?/span>
button.setBackground(shapeDrawable);

在Kotlin中:

val button = findViewById<Button>(R.id.button)
val shapeDrawable = ShapeDrawable(RoundedRectShape(10f, 10f, 10f, 10f)) // 創(chuàng)建圓角矩形
shapeDrawable.paint.color = Color.WHITE // 設(shè)置邊框顏色
shapeDrawable.paint.strokeWidth = 2f // 設(shè)置邊框?qū)挾?/span>
button.background = shapeDrawable
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI