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

来源:互联网 发布:怒蛙网络 编辑:程序博客网 时间:2024/05/18 12:31

最近在写一个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" />

完成啦~

这样就能实现消息提示啦~

阅读全文
0 0
原创粉丝点击