Android 如何在用于抽屉选项的ListView中更改选定ltem的颜色

Android 如何在用于抽屉选项的ListView中更改选定ltem的颜色,android,listview,Android,Listview,我使用的抽屉布局包含一个线性布局,里面是ListView。此LinearLayout用作抽屉,ListView用作抽屉内的选项。我使用了一个名为setItemChecked()的方法,它为在ListView中选择的选项提供默认的粉红色。我想删除默认的粉红色,并想使用我自己的颜色。尝试了很多东西,比如选择器、setSelected(true)、listSelector,都没有效果,甚至有时候所选选项的背景颜色改变了,但是默认的粉红色仍然存在。请帮忙!!! 我无法张贴图像,但这里是代码 <?x

我使用的抽屉布局包含一个线性布局,里面是ListView。此LinearLayout用作抽屉,ListView用作抽屉内的选项。我使用了一个名为setItemChecked()的方法,它为在ListView中选择的选项提供默认的粉红色。我想删除默认的粉红色,并想使用我自己的颜色。尝试了很多东西,比如选择器、setSelected(true)、listSelector,都没有效果,甚至有时候所选选项的背景颜色改变了,但是默认的粉红色仍然存在。请帮忙!!! 我无法张贴图像,但这里是代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context=".ExpenseActivity">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/list_of_drawers"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#FFFFFF"
    >

    <ImageView
        android:layout_marginTop="50dp"
        android:layout_gravity="center"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:textColor="#000"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Version 1.0"
        android:layout_gravity="center"
        android:layout_marginTop="7dp"
        android:textColor="#000"
        android:textSize="12dp"
        android:layout_marginBottom="25dp"/>

    <ListView
        android:id="@+id/list_of_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#FFF"
        android:listSelector="@color/greenBlue"

        />

</LinearLayout>

</androidx.drawerlayout.widget.DrawerLayout>

我只想更改setItemChecked()方法提供的颜色

//ListView的OnClickListener
私有类DrawerItemClickListener实现ListView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//单击项目时的代码
view.setSelected(true);
选择项目(位置);
setActionBarTitle(位置);
}
//使用ARRAYADAPTER将LISTENTRIES数据设置为LISTVIEW
actualList.setAdapter(新阵列适配器,
android.R.layout.simple_list_item_activated_1,listEntries));
//设置setOnItemClickListener以便LISTVIEW侦听我们的单击
setOnItemClickListener(新的DroperItemClickListener());
actualList.setItemChecked(currentPosition,true);

您可以使用触摸选择器 您需要在Drawable文件夹中创建新的xml文件 带选择器的根`

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


<item android:drawable="@color/colorPrimaryLight" android:state_pressed="true" />
<item android:drawable="@color/colorPrimaryLight"android:state_activated="true" />
<item android:drawable="@color/colorPrimaryLight" android:state_selected="true" />


<item android:drawable="@android:color/background_light" /> 
</selector>`

`
Iam假设每个视图都是线性布局 然后,您需要将ListView视图的背景设置为触摸选择器xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_selector">

在drawable中定义list\u activated\u background.xml文件:

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

在AppTheme样式中:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:activatedBackgroundIndicator">@drawable/list_activated_background</item>
</style>

@可绘制/列表\u激活\u背景
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:activatedBackgroundIndicator">@drawable/list_activated_background</item>
</style>