原生NavigationView菜单中添加消息提醒(小红点)

最近在写一个Android项目,项目需求是侧滑栏有个“我的部落”,在“我的部落”可以发布动态,别人可以在我的动态下评论,当有人在我的动态下评论时,侧滑栏“我的部落”右边要出现一个小红点表示提醒:

其实谷歌上有很多关于怎么实现这个效果的文章,但是百度上实在太难找了,写个中文的方便需要的小伙伴吧
  • 实现图中Slideshow那个效果比较简单,附上链接
    https://medium.com/@harivigneshjayapalan/android-adding-badge-or-count-to-the-navigation-drawer-84c93af1f4d9#.fd0xio2ns
  • 实现小红点的效果
    思路:TextView设置小圆点背景
    直接贴代码
    1.写好小红点的布局文件,嵌套在线性布局中是因为在菜单中如果不match_parent的话布局会与顶边对齐,为了居中就再套一个线性布局
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/im_msg"
                android:src="@drawable/prompt_button"
                android:layout_gravity="center_vertical"
                android:layout_width="10dp"
                android:layout_height="10dp"
                android:visibility="gone"/>
        </LinearLayout>
    
2.在menu.xml文件中添加如下代码,在nav_mytribe这个条目添加actionLayout (badge_red_point_2 是小红点的布局文件)
<item
    android:id="@+id/nav_mytribe"
    app:actionLayout="@layout/badge_red_point_2"
    android:icon="@drawable/tribe_button"
    android:title="@string/my_tribe" />
完成啦~
这样就能实现消息提示啦~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值