RadioButton右上角添加红点提醒

本文介绍了如何在RadioButton右上角添加红点作为消息提醒。当遇到老项目使用RadioGroup且无法直接修改布局时,可以使用BadgeView实现这一功能。通过在RadioGroup上方添加一个透明LinearLayout,并结合BadgeView库,轻松完成红点提醒的添加。
摘要由CSDN通过智能技术生成

今天一同学遇到了一个需求设计类似下图的效果,很多人会说简单啊一个framelayout布局就解决了,没错是可以实现该功能需求。但是如果是以前的老项目,运用的RadioGroup就会有一个问题,导航栏根本就不存在布局偷笑。所以这里会谈到另一种解决方案。

该方法运用的是GitHub上一个开源的工具类BadgeView,在RadioGroup的布局之上覆盖一层透明的LinearLayout用于消息提示。废话不多说,让我们一起体验下。


首先你需要把BadgeView拷贝到你的项目中,这里我已经为你准备好了该工具类

package com.example.gu.utils;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TabWidget;

/**
 * A simple text label view that can be applied as a "badge" to any given {@link android.view.View}.
 * This class is intended to be instantiated at runtime rather than included in XML layouts.
 *
 * @author Jeff Gilfelt
 */
public class BadgeView extends AppCompatTextView {

    public static final int POSITION_TOP_LEFT = 1;
    public static final int POSITION_TOP_RIGHT = 2;
    public static final int POSITION_BOTTOM_LEFT = 3;
    public static final int POSITION_BOTTOM_RIGHT = 4;
    public static final int POSITION_CENTER = 5;

    private static final int DEFAULT_MARGIN_DIP = 5;
    private static final int DEFAULT_LR_PADDING_DIP = 5;
    private static final int DEFAULT_CORNER_RADIUS_DIP = 8;
    private static final int DEFAULT_POSITION = POSITION_TOP_RIGHT;
    private static final int DEFAULT_BADGE_COLOR = Color.parseColor("#CCFF0000"); //Color.RED;
    private static final int DEFAULT_TEXT_COLOR = Color.WHITE;

    private static Animation fadeIn;
    private static Animation fadeOut;

    private Context context;
    private View target;

    private int badgePosition;
    private int badgeMarginH;
    private int badgeMarginV;
    private int badgeColor;

    private boolean isShown;

    private ShapeDrawable badgeBg;

    private int targetTabIndex;

    public BadgeView(Context context) {
        this(context, (AttributeSet) null, android.R.attr.textViewStyle);
    }

    public BadgeView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    /**
     * Constructor -
     * <p>
     * create a new BadgeView instance attached to a target {@link android.view.View}.
     *
     * @param context context for this view.
     * @param target  the View to attach the badge to.
     */
    public BadgeView(Context context, View target) {
        this(context, null, android.R.attr.textViewStyle, target, 0);
    }

    /**
     * Constructor -
     * <p>
     * create a new BadgeView instance attached to a target {@link android.widget.TabWidget}
     * tab at a given index.
     *
     * @param context context for this view.
     * @param target  the TabWidget to attach the badge to.
     * @param index   the position of the tab within the target.
     */
    public BadgeView(Context context, TabWidget target, int index) {
        this(context, null, android.R.attr.textViewStyle, target, in
您好,以下是一个自定义的右上角带数字的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"/> ``` 以上仅供参考,您可以根据自己的实际需要进行调整和修改。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值