【发布时间】: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