【问题标题】:My ListView lost its behavior after I changed background color?更改背景颜色后,我的 ListView 失去了行为?
【发布时间】:2014-01-22 20:49:02
【问题描述】:

我所做的只是改变了背景的颜色,当我单击列表项时,我再也无法获得效果。我需要改变什么? XML 如下:

<?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>  

我想要做的只是如下所示,但是当我单击列表项时带有颜色变化:

更新:

<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>

【问题讨论】:

  • 你试过清理项目吗?
  • 请贴出listview的xml代码
  • @VishwasSharma 我有。当我单击时我收到通知,当我这样做时我不再看到突出显示的列表视图
  • @T.V.检查更新。
  • 但是您刚刚将背景颜色设置为列表视图。因此,在开始时它以该颜色显示。您必须在 xml 中将背景设置为 listColor,或者您可以在 ListView 的项目单击时动态更改。

标签: android listview


【解决方案1】:

好吧,我要做的是:
而不是使用固定颜色作为自定义项目的背景,例如

android:background="#FFFFFF">

我会使用 stelist drawable,如下所示:

android:background="@drawable/item_states">

当然,您需要在您的可绘制文件夹中创建一个新的 xml,称为任何名称(在示例中我称之为 item_states)

<?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>

【讨论】:

猜你喜欢
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 2016-01-11
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多