ActionView设置为支持NavigationView中的项未显示 [英] ActionView set to an item in support NavigationView not showing

查看:114
本文介绍了ActionView设置为支持NavigationView中的项未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航抽屉中使用支持的NavigationView来显示项目菜单.

I'm using the support NavigationView in my navigation drawer to display menu of items.

<android.support.design.widget.NavigationView
    android:id="@+id/drawer_navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@drawable/drawable_background"
    app:headerLayout="@layout/drawer_header"
    app:itemBackground="@drawable/drawer_item_background"
    app:itemIconTint="@color/drawer_item"
    app:itemTextColor="@color/drawer_item"
    app:menu="@menu/drawer"
    app:theme="@style/Text.DrawerItem"/>

我想在菜单项旁边显示一个连接请求计数器.

I want to display a counter of connection requests next to a menu item.

我试图像这样在XML菜单定义中设置actionLayout

I've tried to set the actionLayout in XML menu definition like this

<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/navigation_requests"
        android:icon="@drawable/ic_swap_horiz_24dp"
        android:title="@string/connect_menu_connection_requests"
        app:actionLayout="@layout/connection_request_counter"
        app:showAsAction="always"/>
    ...
  </group>
</menu>

ActionView connection_request_counter.xml

Layout of the ActionView connection_request_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:textColor="@color/brand_blue"
      android:text="5"/>

但是目前我看不到该项目旁边的数字5.

But at this point I can't see number 5 next to the item.

我也尝试通过编程方式设置ActionLayout:

I've tried to set ActionLayout programatically as well:

MenuItemCompat.setActionView(mMenu.findItem(R.id.navigation_requests), R.layout.connection_request_counter);

但仍然没有运气.

我可以像这样获得对TextView的引用

I can get reference to the TextView like

final TextView connectionRequestCounterView
            = (TextView) MenuItemCompat.getActionView(mMenu.findItem(R.id.navigation_requests));

实际上返回了一个TextView实例,但是即使我为其设置了文本,也看不到布局中的任何内容.这可能与我在设置菜单项的样式有关吗? (我设置了菜单项的文本颜色,色调和背景)

Which actually returns a TextView instance, but even if I set a text to it, I can't see anything in the layout. Could that be related to the fact that I'm styling the menu items? (I set text color, tint and background of the menu items)

这是我的风格:

<style name="Text.DrawerItem">
    <item name="android:textColor">@color/text_color_dark_grey</item>
    <item name="android:textSize">@dimen/text_size_default</item>
    <item name="android:clickable">false</item>
    <item name="android:textColorLink">@color/text_color_dark_grey</item>
    <item name="fontPath">@string/font_aller_light</item>
    <!--<item name="android:background">@color/background_activity_default</item>-->
    <!--<item name="background">@color/background_activity_default</item>-->
</style>

drawer_item_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!--To override the default grey background for selected item-->
  <item android:drawable="@android:color/transparent" android:state_checked="true"/>
  <item android:drawable="@android:color/transparent"/>
</selector>

drawer_drawablbe.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <shape android:shape="rectangle">
        <solid android:color="@color/background_activity_default"/>
    </shape>
  </item>
  <item
    android:bottom="24dp"
    android:left="16dp">
      <bitmap
        android:gravity="left|bottom"
        android:src="@drawable/my_drawer_logo"/>
  </item>

</layer-list>

其中@drawable/my_drawer_logo是不同密度的PNG.

Where @drawable/my_drawer_logo is a PNG in different densities.

还有其他为什么我看不到动作视图的想法?

Any other ideas why I can't see the action view?

推荐答案

我最终发现了引起此问题的原因. 要使NavigationView正确使用操作视图,必须使用AppCompat支持库版本23.1

I eventually found out what was causing the issue. For NavigationView to work properly with action views, you must use AppCompat support library version 23.1

所以不是

compile 'com.android.support:appcompat-v7:22.3.0'

我必须更新到

compile 'com.android.support:appcompat-v7:23.1.1'

完全正确地显示了花招,并且导航抽屉的导航视图中的动作视图开始正确显示.

which made the trick and the action view in navigation drawer's navigation view started showing properly, exactly as I wanted.

在更新到新的AppCompat版本时,我遇到了更多问题,例如在启动应用程序时出现了ClassNotFoundException,这是通过将所有com.android.support库更新到最新版本来解决的 :

When updating to the new AppCompat version I came across several more problems like ClassNotFoundException showing up when starting the app, which I fixed by updating all com.android.support libraries to the latest version:

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:support-annotations:23.1.1'
...

然后我仍然在标题布局中将NullPointerException设置为NavigationView.如果要设置app:headerLayout="@layout/drawer_header"或类似的代码,则在AppCompat 22版中,可以通过findViewById()获取标题视图.

Then I was still getting NullPointerException in my header layout set to the NavigationView. If you're setting app:headerLayout="@layout/drawer_header" or similarly in code, in AppCompat version 22 it was possible to get the header view by findViewById().

AppCompat 23版对所有项目(包括标题视图)使用RecyclerView,因此,引用其视图的方法如下:

AppCompat version 23, though, uses RecyclerView for all the items including the header view, so the way to get reference to its views is following:

 mHeaderView = navigationView.getHeaderView(HEADER_INDEX);

如果您不添加多个标头,则HEADER_INDEX最可能为0.

Where HEADER_INDEX is most likely 0 if you're not adding multiple headers.

这篇关于ActionView设置为支持NavigationView中的项未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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