您好,登錄后才能下訂單哦!
在Android中,為按鈕(Button)設(shè)計圓角邊框,你可以使用以下幾種方法:
res/drawable
目錄下創(chuàng)建一個新的XML文件,例如rounded_button.xml
。<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>
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"/>
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"/>
你也可以在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
免責(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)容。