【问题标题】:Is there a way to change the background color of a single item from Navigation Drawer?有没有办法从导航抽屉中更改单个项目的背景颜色?
【发布时间】:2018-11-04 21:32:36
【问题描述】:

我有一个导航抽屉菜单,如下所示:

<?xml version"1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:showIn="navigation_view">
    <group android:id="@+id/grp1"       android:checkableBehavior="single">
        <item
            android:id="@+id/nav_my_company"
            android:icon="@mipmap/ic_item_company_layer"
            android:state_checked="false"
            android:visible="false"
            android:title="@string/nav_my_company"/>
        <item
            android:id="@+id/nav_stock"
            android:icon="@mipmap/ic_item_stock_layer"
            android:state_checked="false"
            android:visible="false"
            android:title="@string/nav_stock"/>
        <item
            android:id="@+id/nav_discount"
            android:icon="@mipmap/ic_item_discount_layer"
            android:state_checked="false"
            android:visible="false"
            android:title="@string/nav_discount"/>
    </group>
</menu>

我正在尝试使单个项目具有不同的背景颜色 (android:drawable = "@color/myRedColor"),如下所示:

 <item
            android:id="@+id/nav_stock"
            android:icon="@mipmap/ic_item_stock_layer"
            android:state_checked="false"
            android:visible="false"
            android:drawable = "@color/myRedColor"
            android:title="@string/nav_stock"/>

但我无法得到它。

欢迎任何cmets或建议。

谢谢

【问题讨论】:

标签: android navigation-drawer background-color menuitem


【解决方案1】:

是的 - 您可以创建一个xml 文件并包含在您的导航标签中:

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginTop="?android:attr/actionBarSize">

        <include layout="@layout/a_main_navigation_content" />

    </android.support.design.widget.NavigationView>

a_main_navigation_content.xml 中,您可以使用自定义属性创建textView 或任何您想要的内容。

您将不再需要您的菜单项。

【讨论】:

  • 这就是我正在做的,这不是问题所在。我想更改单个项目的背景颜色,但所有项目的颜色都相同
  • nav_hardware = (MenuItem) menu_principal.findItem(R.id.nav_hardware);
  • 当您在 navMenu 中有 3 个 textView 时,您可以通过调用其 ID 或 Createdobject 来更改第三个或任何您想要的颜色。如果您使用 ExpandableListView 您可以更改 OnChildClickListener 代码中的颜色。
  • 我找不到可以更改项目背景颜色的东西。 pbs.twimg.com/media/DrMObGbW4AEWDQk.jpg
  • 您仍然按照自己的方式行事,请使用除菜单以外的任何视图而不是菜单,例如 TextView sampleNavigationText = findViewById(R.id.yourTextView);像这样的变化:sampleNavigationText .setTextColor(Color.RED);
【解决方案2】:

为了使用Navigation Drawer的好处并使用自定义bg,您需要编写一个自定义导航适配器。然后你可以膨胀任何你想要的布局。创建一个扩展 RecyclerView.Adapter&lt;NavigationAdapter.ViewHolder&gt; 的类。

【讨论】:

  • 简而言之,没那么简单。
【解决方案3】:

如果您想使用项目标签并执行此操作,作为替代方法,您可以尝试BackgroundColorSpanand

s.setSpan(new BackgroundColorSpan(Color.RED), 0, s.length(), 0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2016-05-15
    • 2019-11-25
    • 2016-06-16
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多