![](/img/trans.png)
[英]How can i change the background color of a JList item that is not selected?
[英]How can I change the color of selected item from ListView?
我创建了一个Android项目,其中有一个包含6个项目的列表视图。 我希望每次我从列表中选择任何项目时,它都会变为橙色,直到我按提交按钮为止。
代码是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.SafeWalkApp.SecondActivity" >
<ListView
android:id="@+id/sampleListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:entries="@array/vol_list" >
</ListView>
<Button
android:id="@+id/onsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/submit" />
</RelativeLayout>
这是我的xml代码。 我尝试将颜色添加到color.xml中,但这向我显示了一条错误消息,并且基本上是用于backgroung。 所以请帮我。
好吧,如果您使用的是自定义列表视图,并且通过自定义适配器设置了项,则可以在
nList =(ListView)findViewById(android.R.id.yourlistname); nList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
在上面的函数nList中,是您要进行更改的列表,在其单击列表器中,您可以按其参数作为视图arg1来获取选定的项目视图,从中可以获取线性布局及其所有其他控件,并进行根据需要更改为任何控件颜色,文本等
在这里获取视图并通过获取视图并设置其背景等来设置所需的任何颜色。我无法在此处编写整个代码,但希望您能得到我的?
您必须创建状态可绘制颜色。 还有一些xml.files来根据行的状态定义不同的颜色。
也许这可以帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.