之后,我改变了背景颜色的ListView我失去了它的行为? [英] My ListView lost its behavior after I changed background color?
问题描述
我所做的是改变背景颜色,当我点击列表中的项目,我可以不再获得的效果。我需要做什么改变?的XML如下:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#FFFFFF> < ImageView的
机器人:ID =@ + ID /图像
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:scaleType =centerCrop
机器人:layout_margin =10dp
机器人:SRC =@绘制/文件/> <的EditText
机器人:ID =@ + ID /名称
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignTop =@ + ID /图像
机器人:layout_marginTop =30dp
机器人:layout_toRightOf =@ + ID /图像
机器人:背景=@空
机器人:includeFontPadding =真
机器人:文字颜色=#000000/>
< / RelativeLayout的>
所有我想要做的是有外观如下,但与颜色的变化,当我点击列表项:结果
更新:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。> < ListView的机器人:ID =@ + ID /列表
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#FFFAF0
机器人:dividerHeight =1DP
机器人:layout_margin =0dp
机器人:分=#808080/>< / RelativeLayout的>
那么,I'do是什么:结果
而是采用一个固定的颜色作为背景为您的自定义项目,像
的android:背景=#FFFFFF>
我会使用一个stelist绘制,如:
的android:背景=@绘制/ item_states>
,当然,你需要在你的文件夹中绘制一个新的XML,叫什么(我把它叫做item_states,在本例中)
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =真
机器人:颜色=#FFFF0000/> <! - pressed - >
<项目的android:state_focused =真
机器人:颜色=#FF0000FF/> <! - 集中 - >
<项目的android:颜色=#FF000000/> <! - 默认 - >
< /选择>
All I have done is change the color of the background and I can no longer get the effect when I click the list items. What do I need to change ? The XML is below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_margin="10dp"
android:src="@drawable/file" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/image"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/image"
android:background="@null"
android:includeFontPadding="true"
android:textColor="#000000" />
</RelativeLayout>
All I want to do is to have the look as below but with the change in color when I click the list item:
Update:
<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=".MainActivity" >
<ListView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFAF0"
android:dividerHeight="1dp"
android:layout_margin="0dp"
android:divider="#808080"/>
</RelativeLayout>
Well, what I'do is:
instead of using a fixed color as the background for your custom item, like in
android:background="#FFFFFF">
I'd use a stelist drawable, as in:
android:background="@drawable/item_states">
Of, course, you'll need a new xml in your drawable folder, called whatever (I called it item_states, in the example)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
这篇关于之后,我改变了背景颜色的ListView我失去了它的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!