1. 程式人生 > >原生NavigationView選單中新增訊息提醒(小紅點)

原生NavigationView選單中新增訊息提醒(小紅點)

先看圖


訊息提醒
其實谷歌上有很多關於怎麼實現這個效果的文章,但是百度上實在太難找了,寫個中文的方便需要的小夥伴吧
  • 實現小紅點的效果
    思路: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");

    完成啦~

    獻醜了
    如果你有更好的實現方式請務必告訴我^_^
    微博 黑丫山上小旋風


文/黑丫山上小旋風(簡書作者)
原文連結:http://www.jianshu.com/p/90eb9d06480d
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。