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

来源:互联网 发布:java 接口文档编写 编辑:程序博客网 时间:2024/05/23 05:06

先看图


消息提醒
其实谷歌上有很多关于怎么实现这个效果的文章,但是百度上实在太难找了,写个中文的方便需要的小伙伴吧
  • 实现图中Slideshow那个效果比较简单,附上链接
    https://medium.com/@harivigneshjayapalan/android-adding-badge-or-count-to-the-navigation-drawer-84c93af1f4d9#.fd0xio2ns
  • 实现小红点的效果
    思路:TextView设置小圆点背景
    直接贴代码
    1.写好小红点的布局文件,嵌套在线性布局中是因为在菜单中如果不match_parent的话布局会与顶边对齐,为了居中就再套一个线性布局
    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="wrap_content"  android:layout_height="match_parent"  android:gravity="center_vertical">  <TextView    android:id="@+id/msg"    android:background="@drawable/shape_msg"    android:textColor="@android:color/white"    android:gravity="center"    android:layout_width="20dp"    android:layout_height="20dp"/></LinearLayout>
    2.在menu.xml文件中添加如下代码(badge是小红点的布局文件)
    app:actionLayout="@layout/badge"
    比如要在gallery这个条目添加
    <item  app:actionLayout="@layout/badge"  android:id="@+id/nav_gallery"  android:icon="@drawable/ic_menu_gallery"  android:title="Gallery" />
    3.在代码中添加消息条数
    gallery = (LinearLayout) navigationView.getMenu().findItem(R.id.nav_gallery).getActionView();TextView msg= (TextView) gallery.findViewById(R.id.msg);msg.setText("9");

    完成啦~

    献丑了
    如果你有更好的实现方式请务必告诉我^_^
    微博 黑丫山上小旋风
0 0
原创粉丝点击