Android 自定义导航抽屉中的导航视图仅包含图像视图

Android 自定义导航抽屉中的导航视图仅包含图像视图,android,navigation-drawer,Android,Navigation Drawer,我有一个应用程序,我在其中使用导航抽屉,在此我使用导航视图。代码片段如下所示 <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_gravity=

我有一个应用程序,我在其中使用导航抽屉,在此我使用导航视图。代码片段如下所示

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer"
        />

菜单上有这个 drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/home"
            android:checked="false"
            android:icon="@drawable/home"
            android:title="@string/home_string" />

        <item
            android:id="@+id/offer"
            android:checked="false"
            android:icon="@drawable/offer_"
            android:title="@string/offer_string" />

        <item
            android:id="@+id/take"
            android:checked="false"
            android:icon="@drawable/take"
            android:title="@string/take_string" />

        <item
            android:id="@+id/share"
            android:checked="false"
            android:icon="@drawable/share"
            android:title="@string/share_string" />

        <item
            android:id="@+id/offer_status"
            android:checked="false"
            android:icon="@drawable/my_status"
            android:title="@string/offer_status" />


        <item
            android:id="@+id/about_us"
            android:checked="false"
            android:icon="@drawable/about_us"
            android:title="" />


    </group>
</menu>

这是完美的工作,因为图标显示在左侧,相应的文本显示在导航抽屉图像视图的右侧。 但是现在我必须使用整个图像,在它的右边没有文本,因为我的新图像中也包含名称,所以没有必要将相应的字符串放到它的右边

但是,如果我只是更换图像,他们得到挤压,我认为,因为新的导航菜单有其局限性的图像。如果我从项目中删除字符串,它会给我错误,因为项目必须有标题字符串。所以我放了空字符串,但它的作用是一样的,我的意思是压缩图像


那么我应该怎么做才能在一行完整的导航菜单中显示完整的图像呢??任何帮助和想法请与大家分享

如果您想自定义此菜单,恐怕您必须自己实现此菜单,而不是使用
导航视图
。我个人在
滚动视图
中使用垂直
线性布局
,并将样式应用于所有文本视图和颜色过滤器以着色图标

以下是一些建议:

  • 滚动视图
    中的
    线性布局
    开始,添加可选标题,然后为每个具有适当可绘制视图的节添加一个
    图像视图
  • 在代表一个节的每个
    ImageView
    上设置一个唯一的id
  • 设置单个
    视图。在每个
    ImageView
    上单击Listener
    ,表示一个节。单击视图时,使用视图id确定用户选择了哪个部分
要使用原色绘制当前节,请使用如下代码:

int primaryColor = getResources().getColor(R.color.color_primary);
sectionImageView.getDrawable().mutate().setColorFilter(primaryColor, PorterDuff.Mode.SRC_IN);
(调用
setColorFilter(null)
将其恢复为正常颜色)

如果您想要更详细的代码,请查看“FOSDEM Companion”的源代码:

  • (抽屉布局中包含自定义菜单的布局)
  • (实现菜单逻辑)
  • (使用适配器填充菜单项的自定义线性布局)

那么我应该怎么做,以及如何在导航视图中放置背景图像而不是白色?例如,您可以使用包含
ImageView
s的线性布局。代码的哪一部分对您有帮助?我可以向您指出我编写的自定义菜单实现,但它不能完全满足您的要求,因为它使用文本视图作为菜单项,而您需要的是图像视图。因此,我应该怎么做?请您提供一些代码,否则无法将您的文本视图更改为图像视图