与反NavigationView菜单项右侧 [英] NavigationView menu items with counter on the right

查看:1276
本文介绍了与反NavigationView菜单项右侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的 NavigationView 在新的设计支持库真正伟大的作品。

The new NavigationView in the new Design Support Library works really great.

他们使用菜单项以显示选项。

但我怎么能实现显示的计数右键菜单项?

But how can I achieve to display a count to the right of the menu item?

就像这样的画面:

还是喜欢在的Gmail 应用。

推荐答案

从appcompat-V7 NavigationView 23版开始支持行动的意见,所以还是比较容易实现对付自己。

Starting from version 23 of appcompat-v7 NavigationView supports action views, so it is quite easy to implement counter yourself.

  1. 创建柜台​​布局,即 menu_counter.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" />

  • 引用它在你的抽屉菜单的XML,即菜单/ drawer.xml

    <item
        ...
        app:actionLayout="@layout/menu_counter" />
    

  • 请注意,你应该使用应用命名空间,不要试图用机器人

    Note that you should use app namespace, don't try to use android.

    与另外,您也可以手动设置动作视图 MenuItem.setActionView()方法。

    Alternatively you can manually set action view with MenuItem.setActionView() method.

    查找菜单项进行设置计数器:

    1. Find menu item and set counter:

    private void setMenuCounter(@IdRes int itemId, int count) {
        TextView view = (TextView) navigationView.getMenu().findItem(itemId).getActionView();
        view.setText(count > 0 ? String.valueOf(count) : null);
    }
    

    这篇关于与反NavigationView菜单项右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆