Android 如何更改导航抽屉中的列表选定项颜色?

Android 如何更改导航抽屉中的列表选定项颜色?,android,listview,android-studio,navigation-drawer,Android,Listview,Android Studio,Navigation Drawer,我从Android Studio导航浏览器的示例开始。我正在定制我想要的一切,一切正常,但我找不到如何更改导航抽屉中选定项目的颜色。。。现在它是绿色的,我无法改变这一点 我尝试了一个可绘制的选择器,但没有办法,我设法改变了整个背景,但不只是选定的一个 对不起,如果这是一个noob问题,但我昨天花了几个小时在谷歌和这里没有找到:/ 最后,我终于成功了。 问题是“android.R.layout.simple\u list\u item\u activated\u 1” 它是android内置的

我从Android Studio导航浏览器的示例开始。我正在定制我想要的一切,一切正常,但我找不到如何更改导航抽屉中选定项目的颜色。。。现在它是绿色的,我无法改变这一点

我尝试了一个可绘制的选择器,但没有办法,我设法改变了整个背景,但不只是选定的一个

对不起,如果这是一个noob问题,但我昨天花了几个小时在谷歌和这里没有找到:/


最后,我终于成功了。 问题是“android.R.layout.simple\u list\u item\u activated\u 1”

它是android内置的xml,所以你不能处理你想要的颜色

因此,我创建了一个名为
nav\u drawer\u layout.xml
的文件,其中包含与“android.R.layout.simple\u list\u item\u activated\u 1”文件相同的内容

然后我调整了它,更改了android:background属性,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/nav_drawer_colors"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />
选定的\u抽屉、按下的\u抽屉和背景\u抽屉始终在values\colors.xml中定义

现在一切都好了,除了阴影效果,它在任何地方都有效,但在listview中不起作用^^ 编辑:只需在片段导航抽屉布局(fragment\u navigation\u drawer.xml)中指定背景色,然后删除
nav\u drawer\u colors.xml文件中:)


希望它能帮助其他有同样问题的人:)

通过编码,您可以通过单击ListView上的此项来实现:

int save = -1;
listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    parent.getChildAt(position).setBackgroundColor(
                            Color.parseColor("#A9BCF5"));

                    if (save != -1 && save != position) {
                        parent.getChildAt(save).setBackgroundColor(
                                Color.parseColor("#d6e6ff"));
                    }

                    save = position;
          });
现在将此文件设置为作为xml文件中ListView的Listselector。像

android:listselector="@drawable/select"
或者也用作背景

android:background="@drawable/select"

在drawble中创建xml文件

列表选择器.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>

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

  <gradient
      android:startColor="@color/list_background_pressed"
      android:endColor="@color/list_background_pressed"
      android:angle="90" />

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

 <gradient

      android:startColor="#aed200"

      android:endColor="#aed200"

      android:angle="90" />

</shape>


<ListView
        android:id="@+id/listview_drawer"
        android:layout_width="match_parent"
         android:layout_gravity="start"
        android:layout_height="match_parent"

        android:background="@drawable/list_selector"
        android:dividerHeight="1dp" />

列出项目\u bg\u normal.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>

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

  <gradient
      android:startColor="@color/list_background_pressed"
      android:endColor="@color/list_background_pressed"
      android:angle="90" />

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

 <gradient

      android:startColor="#aed200"

      android:endColor="#aed200"

      android:angle="90" />

</shape>


<ListView
        android:id="@+id/listview_drawer"
        android:layout_width="match_parent"
         android:layout_gravity="start"
        android:layout_height="match_parent"

        android:background="@drawable/list_selector"
        android:dividerHeight="1dp" />

列出已按下的项目。xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>

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

  <gradient
      android:startColor="@color/list_background_pressed"
      android:endColor="@color/list_background_pressed"
      android:angle="90" />

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

 <gradient

      android:startColor="#aed200"

      android:endColor="#aed200"

      android:angle="90" />

</shape>


<ListView
        android:id="@+id/listview_drawer"
        android:layout_width="match_parent"
         android:layout_gravity="start"
        android:layout_height="match_parent"

        android:background="@drawable/list_selector"
        android:dividerHeight="1dp" />

试试这个:

1.在drawable文件夹中创建list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/dark_green" android:state_activated="true" />
<item android:drawable="@color/sliver" android:state_focused="true" />
</selector>

使用列表选择器查看列表谢谢您的回复!当我尝试编程时,它工作正常,但第一次打开抽屉时,它仍然是绿色的。当我改变选择,我回来,它是好的,它的颜色。我找不到初始化好颜色的地方。使用选择器,它仍然不起作用,我可以更改背景,但好像应用程序无法识别选定的状态。你说“现在将此文件设置为xml文件中ListView的Listselector。例如,android:Listselector=“@drawable/select”我不知道在哪里。我试着在我的fragment\u navigation\u drawer.xml中,但是我得到了错误。好的,我放了listselector而不是listselector,所以我得到了错误。我被这个列表视图弄疯了。。。现在,当我按下时,它可以工作,但它会忽略选定的状态。我想这是因为它不是我的布局,它是android中预定义的布局:
mDrawerListView.setAdapter(新的ArrayAdapter(getActionBar().getThemedContext(),android.R.layout.simple_list_item_activated_1,android.R.id.text1,新字符串[]{})谢谢你的回复!不幸的是,应用程序似乎无法识别激活、聚焦或选中的状态。它适用于良好的背景,但所选的仍然是绿色的:/感谢您的回复!不幸的是,应用程序似乎无法识别激活、聚焦或选中的状态。它适用于良好的背景,但所选背景仍然为绿色:/