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

先看图

消息提醒
其实谷歌上有很多关于怎么实现这个效果的文章,但是百度上实在太难找了,写个中文的方便需要的小伙伴吧
<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");

完成啦~

献丑了
如果你有更好的实现方式请务必告诉我_
微博 黑丫山上小旋风

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容