自定义一个右上角带数字的 RadioButton 给出具体的代码
时间: 2024-03-10 13:47:14 浏览: 142
自定义RadioButton
您好,以下是一个自定义的右上角带数字的RadioButton的代码示例,供您参考:
```
public class BadgeRadioButton extends RadioButton {
// 数字的画笔
private Paint mBadgePaint;
// 数字的颜色
private int mBadgeColor;
// 数字的文本
private String mBadgeText;
// 数字的文本颜色
private int mBadgeTextColor;
// 数字的文本大小
private float mBadgeTextSize;
// 数字的文本边距
private float mBadgePadding;
// 数字的半径
private float mBadgeRadius;
// 数字的X轴偏移量
private float mBadgeOffsetX;
// 数字的Y轴偏移量
private float mBadgeOffsetY;
public BadgeRadioButton(Context context) {
super(context);
init(context, null);
}
public BadgeRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public BadgeRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
mBadgePaint = new Paint();
mBadgePaint.setAntiAlias(true);
mBadgeColor = Color.RED;
mBadgeTextColor = Color.WHITE;
mBadgeTextSize = sp2px(context, 10);
mBadgePadding = dp2px(context, 2);
mBadgeRadius = dp2px(context, 6);
mBadgeOffsetX = dp2px(context, 16);
mBadgeOffsetY = dp2px(context, 4);
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BadgeRadioButton);
mBadgeColor = ta.getColor(R.styleable.BadgeRadioButton_badge_color, Color.RED);
mBadgeTextColor = ta.getColor(R.styleable.BadgeRadioButton_badge_text_color, Color.WHITE);
mBadgeTextSize = ta.getDimension(R.styleable.BadgeRadioButton_badge_text_size, sp2px(context, 10));
mBadgePadding = ta.getDimension(R.styleable.BadgeRadioButton_badge_padding, dp2px(context, 2));
mBadgeRadius = ta.getDimension(R.styleable.BadgeRadioButton_badge_radius, dp2px(context, 6));
mBadgeOffsetX = ta.getDimension(R.styleable.BadgeRadioButton_badge_offset_x, dp2px(context, 16));
mBadgeOffsetY = ta.getDimension(R.styleable.BadgeRadioButton_badge_offset_y, dp2px(context, 4));
mBadgeText = ta.getString(R.styleable.BadgeRadioButton_badge_text);
ta.recycle();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBadgeText != null && mBadgeText.length() > 0) {
mBadgePaint.setColor(mBadgeColor);
Rect rect = new Rect();
mBadgePaint.setTextSize(mBadgeTextSize);
mBadgePaint.getTextBounds(mBadgeText, 0, mBadgeText.length(), rect);
float textWidth = mBadgePaint.measureText(mBadgeText);
float badgeWidth = textWidth + 2 * mBadgePadding;
float badgeHeight = rect.height() + 2 * mBadgePadding;
float x = getMeasuredWidth() / 2 + mBadgeOffsetX;
float y = mBadgeOffsetY;
canvas.drawRoundRect(x - badgeWidth / 2, y, x + badgeWidth / 2, y + badgeHeight, mBadgeRadius, mBadgeRadius, mBadgePaint);
mBadgePaint.setColor(mBadgeTextColor);
mBadgePaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(mBadgeText, x, y + badgeHeight - mBadgePadding - rect.bottom, mBadgePaint);
}
}
/**
* 设置数字文本
*/
public void setBadgeText(String text) {
mBadgeText = text;
invalidate();
}
/**
* 设置数字文本颜色
*/
public void setBadgeTextColor(int color) {
mBadgeTextColor = color;
invalidate();
}
/**
* 设置数字文本大小
*/
public void setBadgeTextSize(float size) {
mBadgeTextSize = size;
invalidate();
}
/**
* 设置数字文本边距
*/
public void setBadgePadding(float padding) {
mBadgePadding = padding;
invalidate();
}
/**
* 设置数字半径
*/
public void setBadgeRadius(float radius) {
mBadgeRadius = radius;
invalidate();
}
/**
* 设置数字X轴偏移量
*/
public void setBadgeOffsetX(float offsetX) {
mBadgeOffsetX = offsetX;
invalidate();
}
/**
* 设置数字Y轴偏移量
*/
public void setBadgeOffsetY(float offsetY) {
mBadgeOffsetY = offsetY;
invalidate();
}
private float dp2px(Context context, float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
private float sp2px(Context context, float sp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}
}
```
请注意,这个自定义的RadioButton包含了一些自定义属性,例如`badge_color`、`badge_text_color`、`badge_text_size`等,您可以在xml布局文件中进行设置。具体示例代码如下:
```
<com.example.BadgeRadioButton
android:id="@+id/radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
app:badge_color="#FF0000"
app:badge_text_color="#FFFFFF"
app:badge_text_size="10sp"
app:badge_padding="2dp"
app:badge_radius="6dp"
app:badge_offset_x="16dp"
app:badge_offset_y="4dp"/>
```
以上仅供参考,您可以根据自己的实际需要进行调整和修改。
阅读全文