如何突出ListView控件项 [英] How to highlight ListView-Items
问题描述
我有以下问题。
我有一个由和ImageView的一个TextView定制行一个ListView。 TextView的的XML code是
< TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + /标记
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =26px
机器人:layout_marginLeft =的3px
机器人:单线=真
机器人:ellipsize =结束
机器人:textColorHighlight =#FEC403
/>
然后我有正常工作的itemclicklistener,我想强调的是已被点击通过执行以下TextView的。
公共无效onItemClick(适配器视图<> adaptview,查看clickedview,INT位置,
长ID){
// TODO:行动
字符串pathtofile =(字符串)adaptview.getItemAtPosition(位置);
查看rowview =(查看)adaptview.getChildAt(位置);
rowview.setSelected(真正的);}
我想高亮颜色为#FEC403在XML(即浅橙色),但highlightcolor仍是灰色的。因此如何正确设置highlightcolor?
在此先感谢
编辑:
这是我如何做的最后:
这是我的ListView项目的xml文件:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@可绘制/ rowselector
>
< ImageView的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / musicicon
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:SRC =@可绘制/ musicicon
机器人:以下属性来=的3px
/>
< TextView中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + /标记
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =26px
机器人:layout_marginLeft =的3px
机器人:单线=真
机器人:ellipsize =结束
机器人:可聚焦=假
/>
和rowselector.xml:
< XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目安卓state_selected =真
机器人:可绘制=@色/橙色/>
< /选择器>
和最后我OnItemClick很短的现在:
@覆盖
公共无效onItemClick(适配器视图<> adaptview,查看clickedview,INT位置,
长ID){
// TODO:行动
clickedview.setSelected(真正的);
}
您应该使用选择
。
这问题及其答案可能帮助....
i got the following problem.
i have a ListView with custom rows consisting of an imageview and a textview. the textview's xml code is
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:textColorHighlight="#FEC403"
/>
then i have an itemclicklistener that works fine and i want to highlight the textview that has been clicked by doing the following.
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
long id) {
//TODO: ACTIONS
String pathtofile = (String) adaptview.getItemAtPosition(position);
View rowview = (View) adaptview.getChildAt(position);
rowview.setSelected(true);}
i wanted the highlight color to be "#FEC403" in the xml (that is a light orange) but the highlightcolor still is gray. so how to set the highlightcolor correctly?
thanks in advance
EDIT:
here is how i did it finally:
this is my ListView Item xml file:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/rowselector"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/musicicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/musicicon"
android:paddingLeft="3px"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false"
/>
and the rowselector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/orange" />
</selector>
and at last my OnItemClick is very short now:
@Override
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
long id) {
//TODO: ACTIONS
clickedview.setSelected(true);
}
You should use a Selector
.
This question and its answer might help....
这篇关于如何突出ListView控件项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!