【问题标题】:Navigation view in Custom navigation drawer consist of Image view only自定义导航抽屉中的导航视图仅包含图像视图
【发布时间】:2015-07-26 07:10:05
【问题描述】:

我有一个应用程序,我在其中使用导航抽屉,在此我使用导航视图。代码sn-p如下

<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"
        />

在菜单中我有这个 抽屉.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>

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

但如果我只是替换他们被挤压的图像,我认为是因为新的导航菜单对图像有其限制。如果我从项目中删除字符串,它会给我错误,因为项目必须有标题字符串。所以我放了空字符串,但它的作用相同,我的意思是它挤压图像。

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

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    恐怕如果你想用这个菜单自定义任何东西,你必须自己实现菜单而不是使用NavigationView。我个人在ScrollView 中使用垂直的LinearLayout,并将样式应用于所有TextViews 和ColorFilters 来为图标着色。

    以下是一些建议:

    • ScrollView 中以LinearLayout 开头,为每个具有适当Drawable 的部分添加一个可选标题,后跟一个ImageView
    • 在代表一个部分的每个ImageView 上设置一个唯一的ID。
    • 在代表一个部分的每个ImageView 上设置一个View.OnClickListener。单击视图时,使用视图 ID 来确定用户选择了哪个部分。

    要使用原色为当前部分 Drawable 着色,请使用如下代码:

    int primaryColor = getResources().getColor(R.color.color_primary);
    sectionImageView.getDrawable().mutate().setColorFilter(primaryColor, PorterDuff.Mode.SRC_IN);
    

    (调用setColorFilter(null)将颜色恢复正常)

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

    【讨论】:

    • 那么我应该怎么做以及如何在导航视图中放置背景图像而不是白色?
    • 你可以使用一个包含ImageViews的LinearLayout。
    • 代码的哪一部分对你有帮助?我可以向您指出我编写的自定义菜单实现,但它并不能完全按照您的要求做,因为它使用 TextViews 作为菜单项,而您想要的是 ImageView。
    • 那么我应该怎么做你能提供一些代码
    • 否则无法将您的文本视图更改为图像视图
    猜你喜欢
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    相关资源
    最近更新 更多