在Android中,設置BadgeView的邊框可以通過自定義一個帶有邊框的Drawable,并將其設置為BadgeView的背景。以下是一個簡單的示例:
res/drawable
目錄下創建一個名為badge_border.xml
的文件,內容如下:<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/badge_background"/>
<corners android:radius="8dp"/>
<stroke
android:width="2dp"
android:color="@color/badge_border_color"/>
</shape>
在這個示例中,@color/badge_background
是徽章的背景顏色,@color/badge_border_color
是徽章邊框的顏色。
BadgeView badgeView = new BadgeView(context, null, R.drawable.badge_border);
badgeView.setNumber(5); // 設置徽章數字
badgeView.setNumberTextColor(Color.WHITE); // 設置徽章數字顏色
badgeView.setNumberTextSize(12); // 設置徽章數字字體大小
// 獲取要添加徽章的按鈕
Button button = findViewById(R.id.button);
// 將BadgeView添加到按鈕上
badgeView.attachToTarget(button);
這樣,你就可以設置BadgeView的邊框了。請確保你已經將所需的顏色資源添加到res/values/colors.xml
文件中。