美文网首页安卓
NavigationView MenuItem中添加新布局

NavigationView MenuItem中添加新布局

作者: 莫里亚蒂教授 | 来源:发表于2019-03-05 10:10 被阅读0次

· 实现Slideshow比较简单的效果

image.png

Navigation drawer in an app like Gmail will have a badge or count value in its row element. Here is a simple way to add them.
Android Studio (latest version) will allow us to create ‘Helloworld’ apps with material design guidelines. While creating a new android project, choose “Navigation Drawer Activity”.(AS最新版本将允许我们使用材料设计指南创建“Helloworld”应用程序)
Studio will now create an Activity with a navigation drawer. Follow the steps below to add the badges.

第1步:将“actionViewClass”属性添加到navigation drawer menu

在使用导航抽屉创建“Hello world”应用程序后,在项目层次结构视图的“菜单”文件夹下查找文件“activity_main_drawer.xml”(即yourActivityName_drawer.xml)。

Identify the group item and add “app:actionViewClass=android.widget.TextView” as given below:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto">
     <group android:checkableBehavior="single">
         <item
             android:id="@+id/nav_camera"
             android:icon="@drawable/ic_menu_camera"
             android:title="Import" />
         <item
             android:id="@+id/nav_gallery"
            app:actionViewClass="android.widget.TextView"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            app:actionViewClass="android.widget.TextView"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
      </group>
</menu> 

步骤2:声明Navigation Drawer menu item并使用badge值初始化item

In your main Activity, declare the menu item of the Navigation Drawer as given below

//Create these objects above OnCreate()of your main activity
TextView slideshow,gallery;
//These lines should be added in the OnCreate() of your main activity
gallery=(TextView) MenuItemCompat.getActionView(navigationView.getMenu().
        findItem(R.id.nav_gallery));
slideshow=(TextView) MenuItemCompat.getActionView(navigationView.getMenu().
        findItem(R.id.nav_slideshow));
//This method will initialize the count value
initializeCountDrawer();

initializeCountDrawer()可以在需要的地方调用。它还可用于更新navigation drawer menu item中的计数或badge值。

private void initializeCountDrawer(){
    //Gravity property aligns the text
    gallery.setGravity(Gravity.CENTER_VERTICAL);
    gallery.setTypeface(null, Typeface.BOLD);
    gallery.setTextColor(getResources().getColor(R.color.colorAccent));
    gallery.setText("99+");
    slideshow.setGravity(Gravity.CENTER_VERTICAL);
    slideshow.setTypeface(null,Typeface.BOLD);                
    slideshow.setTextColor(getResources().getColor(R.color.colorAccent));
    //count is added     
    slideshow.setText("7");
}

Dynamic badge values

如果需要动态标记值,例如更新API调用或SQLite数据库中的值,请创建可重用的方法并在Activity 的OnStart()或OnResume()方法上更新它。

完整的源代码可以在以下链接中找到: https//github.com/Hariofspades/NavigationDrawerLabel

· 实现小红点效果效果

image1.png

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");

相关文章

网友评论

    本文标题:NavigationView MenuItem中添加新布局

    本文链接:https://www.haomeiwen.com/subject/aatnuqtx.html