效果图

写一篇短小精悍,好用的知识积累吧。开发中时常会出现信息提醒,新内容提示等等一堆问题。其实就是在各种控件或者是item上面加“小圆点”。网上一搜一大堆。。。但是感觉说的好多。我们只需要基本功能2333.
下面介绍三种方式吧,但是大体套路相同。

一、解需求思路

在 RadioGroup 的 RadioButton 上面直接加小圆点,对于我来说实现有点困难,因为我下面还有文字。搞不好,文字就挤没了。 所有我现在在原有的 RadioGroup 上面加一层覆盖物,类似于 ui 常常接触的图层。

二、布局

就是在原有的 RadioGroup 上覆盖一层透明的 button 并且保证位置相同相同

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorGrayBg"tools:context="cn.ln80.happybirdcloud119.activity.MainActivity"><android.support.v4.view.ViewPagerandroid:id="@+id/vp_main_page"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="61dp" /><!--底栏--><RadioGroupandroid:id="@+id/rg_main_bottom"android:layout_width="match_parent"android:layout_height="@dimen/iconsize_60"android:layout_alignParentBottom="true"android:background="@android:color/white"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rb_main_task"style="@style/BottomRadio"android:drawableTop="@drawable/bg_bottom_task"android:text="可视化"android:textColor="@drawable/txt_bottom_notify" /><RadioButtonandroid:id="@+id/rb_main_notify"style="@style/BottomRadio"android:layout_height="match_parent"android:drawableTop="@drawable/bg_bottom_notify"android:text="通知"android:textColor="@drawable/txt_bottom_notify" /><RadioButtonandroid:id="@+id/rb_main_home"style="@style/BottomRadio"android:layout_marginTop="@dimen/widget_margin_5"android:checked="true"android:drawableTop="@drawable/bg_bottom_home"android:text="主页"android:textColor="@drawable/txt_bottom_notify" /><RadioButtonandroid:id="@+id/rb_main_inspect"style="@style/BottomRadio"android:drawableTop="@drawable/bg_bottom_inspect"android:text="巡检"android:textColor="@drawable/txt_bottom_notify" /><RadioButtonandroid:id="@+id/rb_main_mine"style="@style/BottomRadio"android:drawableTop="@drawable/bg_bottom_mine"android:text="我的"android:textColor="@drawable/txt_bottom_notify" /></RadioGroup><LinearLayoutandroid:layout_width="match_parent"android:layout_height="@dimen/iconsize_60"android:layout_alignParentBottom="true"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_main_task"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@null" /><Buttonandroid:id="@+id/btn_main_notification"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@null" /><Buttonandroid:id="@+id/btn_main_home"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@null" /><Buttonandroid:id="@+id/btn_main_inspect"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@null" /><Buttonandroid:id="@+id/btn_main_my"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@null" /></LinearLayout>
</RelativeLayout>

三、第一种方式

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabWidget;/*** Content:* Actor:韩小呆 ヾ(゚▽゚)ノ* Time:  2018/8/6 17:47* Update:* Time:*/
public class BadgeView extends android.support.v7.widget.AppCompatTextView {private boolean mHideOnNull = true;public BadgeView(Context context) {this(context, null);}public BadgeView(Context context, AttributeSet attrs) {this(context, attrs, android.R.attr.textViewStyle);}public BadgeView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}private void init() {if (!(getLayoutParams() instanceof LinearLayout.LayoutParams)) {LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.END | Gravity.TOP);setLayoutParams(layoutParams);}// set default fontsetTextColor(Color.WHITE);setTypeface(Typeface.DEFAULT_BOLD);setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);setPadding(dip2Px(5), dip2Px(1), dip2Px(5), dip2Px(1));// set default backgroundsetBackground(9, Color.parseColor("#d3321b"));setGravity(Gravity.CENTER);// default valuessetHideOnNull(true);setBadgeCount(0);}public void setBackground(int dipRadius, int badgeColor) {int radius = dip2Px(dipRadius);float[] radiusArray = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);bgDrawable.getPaint().setColor(badgeColor);setBackground(bgDrawable);}/*** @return Returns true if view is hidden on badge value 0 or null;*/public boolean isHideOnNull() {return mHideOnNull;}/*** @param hideOnNull the hideOnNull to set*/public void setHideOnNull(boolean hideOnNull) {mHideOnNull = hideOnNull;setText(getText());}/** (non-Javadoc)** @see android.widget.TextView#setText(java.lang.CharSequence, android.widget.TextView.BufferType)*/@Overridepublic void setText(CharSequence text, BufferType type) {if (isHideOnNull() && (text == null || text.toString().equalsIgnoreCase("0"))) {setVisibility(View.GONE);} else {setVisibility(View.VISIBLE);}super.setText(text, type);}public void setBadgeCount(int count) {setText(String.valueOf(count));}public Integer getBadgeCount() {if (getText() == null) {return null;}String text = getText().toString();try {return Integer.parseInt(text);} catch (NumberFormatException e) {return null;}}public void setBadgeGravity(int gravity) {FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();params.gravity = gravity;setLayoutParams(params);}public int getBadgeGravity() {FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();return params.gravity;}public void setBadgeMargin(int dipMargin) {setBadgeMargin(dipMargin, dipMargin, dipMargin, dipMargin);}public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin) {FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();params.leftMargin = dip2Px(leftDipMargin);params.topMargin = dip2Px(topDipMargin);params.rightMargin = dip2Px(rightDipMargin);params.bottomMargin = dip2Px(bottomDipMargin);setLayoutParams(params);}public int[] getBadgeMargin() {FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();return new int[]{params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin};}public void incrementBadgeCount(int increment) {Integer count = getBadgeCount();if (count == null) {setBadgeCount(increment);} else {setBadgeCount(increment + count);}}public void decrementBadgeCount(int decrement) {incrementBadgeCount(-decrement);}/** Attach the BadgeView to the TabWidget** @param target the TabWidget to attach the BadgeView** @param tabIndex index of the tab*/public void setTargetView(TabWidget target, int tabIndex) {View tabView = target.getChildTabViewAt(tabIndex);setTargetView(tabView);}/** Attach the BadgeView to the target view** @param target the view to attach the BadgeView*/public void setTargetView(View target) {if (getParent() != null) {((ViewGroup) getParent()).removeView(this);}if (target == null) {return;}if (target.getParent() instanceof FrameLayout) {((FrameLayout) target.getParent()).addView(this);} else if (target.getParent() instanceof ViewGroup) {ViewGroup parentContainer = (ViewGroup) target.getParent();int groupIndex = parentContainer.indexOfChild(target);parentContainer.removeView(target);FrameLayout badgeContainer = new FrameLayout(getContext());ViewGroup.LayoutParams parentLayoutParams = target.getLayoutParams();badgeContainer.setLayoutParams(parentLayoutParams);target.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));parentContainer.addView(badgeContainer, groupIndex, parentLayoutParams);badgeContainer.addView(target);badgeContainer.addView(this);} else if (target.getParent() == null) {Log.e(getClass().getSimpleName(), "ParentView is needed");}}/** converts dip to px*/private int dip2Px(float dip) {return (int) (dip * getContext().getResources().getDisplayMetrics().density + 0.5f);}
}

将这个代码直接 C、V 到自己的 类内,然后在使用的地方直接填写如下代码:

   BadgeView badgeView = new BadgeView(this);badgeView.setTargetView(btnMainNotification);badgeView.setBadgeCount(12);badgeView.setBackground(20, getResources().getColor(R.color.colorRed));

这个自定义的代码是一位外国大佬搞出来的,原版代码在这,欢迎查阅:BadgeView。

这里面的一些属性:

    //设置依附的控件setTargetView(View) //设置显示的数字setBadgeCount(int) //设置显示的位置setBadgeGravity(Gravity) //设置背景色setBackgroundColor() //设置背景图片setBackgroundResource() //设置显示字体  比如说加粗、倾斜、啥的setTypeface() //设置字体阴影setShadowLayer()

四、第二种方式

引用这位大佬的依赖 :BadgeView ---> compile 'q.rorbin:badgeview:1.1.3'

代码代码中使用:

new QBadgeView(this).bindTarget(btnMainNotification).setBadgeText("99+").setBadgeBackgroundColor(getResources().getColor(R.color.colorRed));

可以返回一个 Badge 对象提高开发者操作。

一些常用属性

          //设置Badge数字setBadgeNumber(int i);//设置Badge文本setBadgeText(String text);  //设置文本字体大小setBadgeTextSize(int size); // 设置文本颜色setBadgeTextColor();//设置是否显示精确模式数值setExactMode();//设置Badge相对于TargetView的位置setBadgeGravity(); //设置外边距setGravityOffset();//设置内边距setBadgePadding();//设置背景色setBadgeBackgroundColor(); //设置背景图片setBadgeBackground();//设置是否显示阴影setShowShadow();//打开拖拽消除模式并设置监听setOnDragStateChangedListener();//描边stroke();//隐藏Badgehide();
官方的演示

五、最后一种实现方式

BGABadgeView-Android

六、总结

总体的实现思路就是,在底部导航栏覆盖一层透明的布局,然后在透明布局内的控件上面依赖(寄生)上小圆点。

Android 底部导航栏添加消息数目提示相关推荐

  1. android底部导航栏带消息数的框架,GitHub - BarkSheep/Android-NavMenuLayout: 一个底部导航栏, 实现了显示未读消息数, 显示红点等效果的封装...

    Android-NavMenu-master 一个底部导航栏, 实现了显示未读消息数, 显示红点等效果的封装. 添加依赖 1. 在项目根目录的 build.gradle 中添加 allprojects ...

  2. Android底部导航栏+消息提醒

    Android底部导航栏+消息提醒 最近想在网上找一些Android底部导航栏切换并能提供消息提醒的案例,虽然有很多案例但都不是我想要的.我就开始自己瞎研究了,废话不多说了,直接上代码. 1.先创建一 ...

  3. android fragment 底部菜单栏,一句话搞定Android底部导航栏,一键绑定Fragment、ViewPager...

    现在大多数App都会用到底部导航栏,比如常见的聊天工具QQ.微信.购物App等等,有了底部导航栏,用户可以随时切换界面,查看不同的内容.它的实现方式也很多,以前大多使用TabHost来实现,但是现在我 ...

  4. Android 9 导航栏添加截图按钮

    Android 9 导航栏添加截图按钮 功能:Android 系统底部导航栏(HOME\BACK)位置添加截图按钮,系统设置界面添加截图开关 主要修改工程有SystemUI 和 Settings 注意 ...

  5. Android底部导航栏最常用的两种写法

    先来看看底部导航栏的效果 Android 底部导航栏有很多种写法,例如: RadioGroup , Tablayout, TabHost  , LinearLayout + ImageView + T ...

  6. android 固定底部导航,如何设置android底部导航栏位置固定在android

    请帮我设置底部导航栏位置固定在底部, ,因为我在输入editText字段时遇到问题,底部导航栏向上移动并覆盖其他领域如何设置android底部导航栏位置固定在android 代码: xmlns:and ...

  7. 转载:Android底部导航栏,三种风格和实现

    原文出处 标题:Android底部导航栏,三种风格和实现 作者:阿飞__ 原文链接:Android底部导航栏,三种风格和实现_阿飞__的博客-CSDN博客_android导航栏 一.效果图展示 如果动 ...

  8. Android底部导航栏的三种风格实现

    一.效果图展示 如果动图没有动的话,也可以看下面这个静态图 以下挨个分析每个的实现,这里只做简单的效果展示,大家可以基于目前代码做二次开发. 二.BottomNavigationView 这是 Goo ...

  9. Android底部导航栏切换页面填坑

    ** Android底部导航栏切换页面填坑 ** 这个效果的实现关键点就是给选项赋予两种状态,focused和normal,在主程序中用监听判断是否被选中,就给被选中的选项设focused为true, ...

最新文章

  1. JSP访问Hadoop 图片存储服务
  2. ArrayList遍历的同时删除
  3. 正常配置文件实例模板
  4. 生产者消费者---线程管道
  5. 杭电 OJ 提交代码需要注意的问题
  6. Atitit 数据库事务实现原理
  7. 内存超频时序怎么调_超频技术之内存“时序”重要参数设置解说
  8. html怎么画虚线空心圆,PS怎么画虚线圆圈 一个工具轻松搞定
  9. Moudle、aar新建和导入
  10. Java编程那些事儿78——时间和日期处理
  11. Python 爬虫下载喜马拉雅音频文件
  12. python简易版爬虫
  13. Windows10ISO 22h2-19045.2006原版系统镜像2022年9月份版
  14. 关于身份认证中的Authenticator及AuthenticationStrategy
  15. 解决SVN左下角图标不显示(图文详解)
  16. 计算机网络速成课【体系结构】
  17. 基于词向量word2vec匹配的英文问答系统
  18. 强烈推荐优秀英文网站
  19. 2021年煤气考试报名及煤气考试APP
  20. python 抽奖 配音乐_抖音上超好听的神曲音乐,Python教你一次性下载

热门文章

  1. Quartz-Job 详解
  2. Quartz- Quartz API以及Jobs 和Triggers介绍
  3. csv java web 导入_DAY5-step9 使用CSV模块和Pandas在Python中读取和写入CSV文件
  4. 学习笔记——Numpy基本操作(二)
  5. 使用android studio查看内存,Android Studio Profiler使用心得 检测内存泄露问题
  6. poi获取段落位置_Apache POI:从java中的word文档(docx)中提取段落和后续表格
  7. 多元经验模态分解_【Applied Energy最新原创论文】一个基于多元搜索引擎数据的多尺度油价预测方法...
  8. PyCharm配置anaconda环境 安装第三方库
  9. oracle sys 查询语句,Oracle EBS-SQL (SYS-7):表单个性化查询.sql
  10. jquery和php怎么链接地址,jQuery操作url地址(附代码)