所选项目的导航抽屉项目背景颜色 [英] Navigation Drawer item background colour for selected item

查看:24
本文介绍了所选项目的导航抽屉项目背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Android Studio 来实现 Navigation Drawer,但我无法获得用于显示我们当前要更改的部分的蓝色.

I used Android Studio to implement the Navigation Drawer and I can't get the blue colour that is used to show which section we're currently in to change.

我尝试了很多东西,我目前使用的是一个 listSelector,它看起来像:

I've tried numerous things, I'm currently using a listSelector which looks like:

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

    <item android:state_activated="true" android:drawable="@color/selected" />
    <item android:state_pressed="true" android:drawable="@color/highlight" />


</selector>

我也试过state_checked.state_pressed 在这种情况下有效,但当前选择的项目仍然是蓝色的.

I've also tried state_checked. state_pressed works in this situation but the currently selected item is still blue.

我一直在研究这个,当创建适配器时,传递的上下文是 getActionBar().getThemedContext() 所以我在想我是否能找到正确的属性来分配给我的操作栏风格我可以从那里改变它.我尝试了一些不同的属性,但没有运气.有人知道确切的属性吗?

I've been examining this more and when the adapter is created the context that is passed is getActionBar().getThemedContext() so I'm thinking if I can find the right attribute to assign to my actionbar style I can change it from there. I've tried a few different attributes with no luck. Does anyone know the exact attribute?

我也意识到如果我把

<item name="android:activatedBackgroundIndicator">@drawable/nav_listview_selector</item>

在我的主题的主要部分并更改 getActionBar().getThemedContext()getActivity.getBaseContext 然后我可以更改颜色,但我不这么认为是正确的方法.我认为应该使用主题上下文.因此,如果有人知道 activatedBackgroundIndicator 可以放在哪里,以便在 getActionBar.getThemedContext()

in the main part of my theme and change getActionBar().getThemedContext() for getActivity.getBaseContext then I can change the color but I don't think this is the correct way. I think the themed context should be used. So if anyone knows where the activatedBackgroundIndicator could be put so that it would be used in getActionBar.getThemedContext()

编辑 2:

所以用于列表视图的文本视图是 SDK 中的一个,它看起来像这样:

So the text view used for the listview is one within the SDK it looks like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

所以我尝试在主题级别修改 "?android:attr/activatedBackgroundIndicator" 但它对选中/选择/激活没有影响,但对按下有影响.有人知道为什么是这样吗?我该如何改变它?

So I tried modifying the "?android:attr/activatedBackgroundIndicator" at the theme level but it has no effect for checked/selected/activated but it does for pressed. Does anyone know why this is? And how I can change it?

推荐答案

仍然不知道为什么它不起作用.但我发现它的方法是使用我自己的 simple_list_item_activated 布局传递给 ArrayAdapter,除了将文本颜色设置为白色之外,它基本上是相同的.然后我用 getActivity().getBaseContext() 替换了 getActionBar().getThemedContext() 并且它现在有效果了.

Still not sure why it is that it doesn't work. But the way I found around it is to use my own simple_list_item_activated layout to be passed to the ArrayAdapter which was basically the same except for setting the text colour to white. I then replaced getActionBar().getThemedContext() with getActivity().getBaseContext() and it now has an effect.

这可能不是正确的方式,可能会在未来产生影响,但现在我让它按照我想要的方式工作.

This may not be the correct way and may have repercussions in the future, but for now I have it working the way I want it to.

这篇关于所选项目的导航抽屉项目背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆