首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android NavigationView双textColor

Android NavigationView双textColor
EN

Stack Overflow用户
提问于 2016-04-28 16:20:05
回答 1查看 39关注 0票数 0

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

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

有什么办法可以推翻这种行为吗?

我的导航视图菜单:

代码语言:javascript
复制
<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中的主题

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-29 14:54:36

好的,这个很管用,但它真的很脏。I< 16表示它只应更改前4项的背景。

将其添加到onCreate方法的末尾

代码语言:javascript
复制
 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;
            }
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36920272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档