【问题标题】:Android NavigationView dual textColorAndroid NavigationView双textColor
【发布时间】:2016-04-28 16:20:05
【问题描述】:

我正在尝试使用两组导航视图,其中第一组 textColor 为黑色,第二组为 lightGrey。

我似乎只能在 NavigationView xml 配置中指定一种颜色。

有没有办法覆盖这种行为?

我的导航视图菜单:

<group
    android:id="@+id/navGroupMain"
    android:checkableBehavior="single">
    <item
        android:id="@+id/nav_dashboard"
        android:icon="@drawable/ic_dashboard"
        android:title="@string/navigation_drawer_item_dashboard" />
</group>

<group
    android:id="@+id/navGroupFooter"
    android:checkableBehavior="single">
    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_settings"
        android:title="@string/navigation_drawer_item_settings" />
</group>

这是插入到 NavigationView 中的主题

<style name="AppTheme.ThemeOverlay.NavigationView" parent="ThemeOverlay.AppCompat.Light">
    <!-- This is the menu item text color. -->
    <item name="android:textColorPrimary">@color/black</item>
    <!-- This is the menu header text color and icon color. -->
    <item name="android:textColorSecondary">@color/grey_500</item>
    <!-- This is the selected color. -->
    <item name="colorPrimary">@color/colorPrimary</item>
</style>

【问题讨论】:

  • 请贴一些代码
  • 我的代码有效,请接受它作为答案!

标签: android


【解决方案1】:

好吧,这行得通,但它真的很脏。 i[0]

将它添加到 onCreate 方法的末尾

 LayoutInflater layoutInflater = getLayoutInflater();
      try {
          Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
          field.setAccessible(true);
          field.setBoolean(layoutInflater, false);
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      } catch (NoSuchFieldException e) {
          e.printStackTrace();
      }
        final int[] i = {0};
        LayoutInflaterCompat.setFactory(getLayoutInflater(), new LayoutInflaterFactory() {
            @Override
            public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
                i[0]++;
                if (i[0] < 16 && name .equalsIgnoreCase("android.support.design.internal.NavigationMenuItemView")) {
                    try{
                        LayoutInflater f = getLayoutInflater();
                        final View view = f.createView(name, null, attrs);
                        new Handler().post(new Runnable() {
                            public void run() {
                                // set the background drawable
                                view .setBackgroundColor(Color.CYAN);

                            }
                        });
                        return view;
                    } catch (InflateException e) {
                    } catch (ClassNotFoundException e) {}
                }
                return null;
            }
        });

【讨论】:

    猜你喜欢
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2017-10-06
    • 1970-01-01
    • 2016-04-05
    • 2020-01-15
    相关资源
    最近更新 更多