如何改变Android的列表项的颜色上点击或选择? [英] how to change color of android list item on click or selection?
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/4247385/how-to-change-color-of-listview-items-on-focus-and-on-click">How改变ListView的项目颜色焦点和点击
朋友,
我想改变颜色上的android列表项点击 任何一个指导我哪个属性我应该用它来实现这个功能?
其实,他并不知道用户点击列表项,如果它被点击或不?
这是我的code。
&LT; ListView的机器人:ID =@ + ID / Android的:清单机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT机器人:drawSelectorOnTop =假
机器人:layout_below =@ + ID / Tablayoutdesign
机器人:cacheColorHint =#000000
机器人:dividerHeight =1dip
机器人:layout_marginTop =63dip
机器人:layout_marginBottom =40dip
/&GT;
第一步:Embbed了android:如下图所示在你的ListView listSelector属性
&LT; ListView的机器人:ID =@ + ID / Android的:清单机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / Tablayoutdesign
机器人:cacheColorHint =#000000
机器人:dividerHeight =1dip
机器人:layout_marginTop =63dip
机器人:layout_marginBottom =40dip
/&GT;
第二步:创建一个新的XML命名listselector,把下面的code在
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;选择的xmlns:机器人=http://schemas.android.com/apk/res/android&GT;
&LT;! - 选择 - &GT;
&LT;项目
机器人:state_focused =真
机器人:state_selected =假
机器人:可绘制=@可绘制/聚焦/&GT;
&LT; - !pressed - &GT;
&LT;项目
机器人:state_selected =真
机器人:state_focused =假
机器人:可绘制=@可绘制/选择/&GT;
&LT; /选择器&GT;
第三步:创建一个名为colors.xml文件 添加以下的code在该文件中
&LT;资源&GT;
&LT;绘制NAME =专注与GT;#ff5500&LT; /绘制&GT;
&LT;#FF00FF&LT; /绘制&GT =选择&GT绘制的名字;
&LT; /资源&GT;
添加此行的你的java code
的ListView LV =(ListView控件)findViewById(R.id.list);
lv.setSelector(R.drawable.listselector);
Possible Duplicate:
How to change color of ListView items on focus and on click
friends,
i want to change color on android list item click any one guide me which attribute should i use to achieve this functionality?
actually when user clicks on list item he does not know if it was clicked or not?
here is my code.
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:drawSelectorOnTop="false"
android:layout_below="@+id/Tablayoutdesign"
android:cacheColorHint="#000000"
android:dividerHeight="1dip"
android:layout_marginTop="63dip"
android:layout_marginBottom="40dip"
/>
Step1: Embbed the android:listSelector attribute in your ListView as Shown Below.
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Tablayoutdesign"
android:cacheColorHint="#000000"
android:dividerHeight="1dip"
android:layout_marginTop="63dip"
android:layout_marginBottom="40dip"
/>
Step2: Create a new xml named listselector and put the following code in that
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="@drawable/focused"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="@drawable/selected" />
</selector>
Step3: Create a file named colors.xml add the following the code in that file
<resources>
<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>
Add this line in your java code
ListView lv= (ListView) findViewById(R.id.list);
lv.setSelector( R.drawable.listselector);
这篇关于如何改变Android的列表项的颜色上点击或选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!