【问题标题】:ListView selector color selects all object, not each itemListView 选择器颜色选择所有对象,而不是每个项目
【发布时间】:2025-12-18 11:55:01
【问题描述】:

我已经完成了一个 ListView 并用一个自定义适配器填充它,现在我想用不同的颜色显示选择了什么元素,所以我用这个代码制作了一个 drawer_list_selection.xml:

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

然后,我将我的 ListView 背景设置为这个文件:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
    android:layout_height="match_parent" android:choiceMode="singleChoice"
    android:divider="@android:color/transparent" android:dividerHeight="0dp"
    tools:context=".NavigationDrawerFragment" android:id="@+id/section_list"
    android:background="@drawable/drawer_list_selector" />

我的问题是,当我按下一个部分(一个元素)时,整个 ListView 转为 @color/primary 并且激活的方法都不起作用。有什么解决办法吗?也许是我的适配器的问题?顺便说一句:

public class adaptadorLista extends ArrayAdapter<Secciones>{
    public adaptadorLista(Context context, Secciones[] section){
        super(context,0,section);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View item = inflater.inflate(R.layout.fragment_main, null);

        ImageView img= (ImageView) item.findViewById(R.id.imageView_section);
        img.setImageResource(section[position].getIcon());
        TextView txt_section = (TextView) item.findViewById(R.id.section_label);
        Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Jaapokki-Regular.otf");
        txt_section.setTypeface(tf);
        txt_section.setText(section[position].getSec());
        return (item);
    }
}

视图的自定义 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/drawer_list_selector"
    tools:context=".MainActivity$PlaceholderFragment"
    >
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp" android:background="@drawable/drawer_list_selector">
        <TableRow android:layout_gravity="center_horizontal|center_vertical"
            android:layout_width="fill_parent">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_marginRight="5sp"
                android:id="@+id/imageView_section" android:src="@drawable/ic_d" android:scaleType="fitCenter"
                android:maxHeight="20dp" android:maxWidth="20dp" android:adjustViewBounds="true"/>
            <TextView android:id="@+id/section_label" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="hi" android:textSize="20sp"
                android:textColor="#000"
            />
        </TableRow>
    </TableLayout>
</LinearLayout>

【问题讨论】:

  • 将您的选择器设置为 android:listSelector。不是列表视图背景
  • 对不起,它不起作用...只为元素着色,而不是总宽度
  • 从您的ListView 中删除此行android:background="@drawable/drawer_list_selector"。您只需要为项目而不是整个列表添加选择器。
  • 是的,我已经做到了,但是现在,如果我将背景放在自定义视图的元素上(在本例中为 TextView),则只会为 TextView 的空间着色,而不是整个宽度列表显示。 TextView layout_width 设置为填充父级,理论上避免这种情况。谢谢。
  • 不,paddingLeft 将图标与列表的边缘分开。事实上,视图中没有着色的部分是正确的部分。感谢您的帮助

标签: android listview android-listview colors selector


【解决方案1】:

希望您的适配器视图有一个父布局。

所以将android:background="@drawable/drawer_list_selector" 给布局或ImageView 或TextView。

如果布局中有 ImageView 和 TextView,则可以为该布局设置背景。

希望对你有所帮助。

【讨论】:

  • 它不工作。我有一个 LinearLayout,里面有一个 TableLayout,里面有一个 TextView 和 ImageView,当我将 android:background 属性设置为 TextView/ImageView(或整个 TableLayout 或 LinearLayout)时,颜色仅在元素上而不是在整个ListView 宽度,我在 fill_parent 处拥有所有控件宽度...
  • 不要为您的表格布局提供 paddingleft。
  • 右边的部分是没有上色的部分。 paddingLeft 用于将元素与屏幕边框分开
  • 将 paddingleft 赋予 imageview 并在此基础上调整 textview。否则将 tableview 放在 relativelayout 中并使相对宽度匹配父级。同时删除 tableview 的背景并将其设置为 relativelayout。
  • 完美!非常感谢!
【解决方案2】:

你需要删除这些行。

<item android:state_activated="true" android:drawable="@color/primary" />
android:background="@drawable/list_selector"

并且设置列表选择器不是背景

android:listSelector="@drawable/yourlist_selector"

并在 xml 中的 listView 中设置属性 android:drawSelectorOnTop="true"

【讨论】:

  • 不,它会从 listView 中删除所选项目,并且仍然只为元素着色,而不是 listview 的宽度