java - 设置 ListView 背景时应用程序崩溃

标签 java android eclipse listview

我正在尝试为我的应用程序创建一种卡片布局。在activity_events下,我需要将list_row设置为列表中每个项目的背景。如下所示,应用程序在调用 Activity 时崩溃。但是仅删除 ListView android:background="@layout/list_row" 会导致一切正常,只是看起来不正确。我已经尝试将 list_row 移动到可绘制文件夹并使用 @drawable/list_row ,但这也不起作用。 ListView 中的背景是否有一些我不明白的限制?

下面是activity_events.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

    <ListView
            android:id="@+id/android:list"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"    
        android:dividerHeight="0dp"
        android:background="@layout/list_row">
    </ListView>

    <ProgressBar
        android:id="@+id/eventsProgressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" />

    </LinearLayout>

下面是list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left side Thumbnail image -->
<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" 
    android:orientation="vertical">
   <ImageView
        android:id="@+id/list_image"
        android:contentDescription="@string/app_name"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:src="@drawable/barjinx_logo" /> <!--Removal makes it work!-->
</LinearLayout>

下面是list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
    android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/gradient_bg_hover" />

最佳答案

无法使用布局作为背景。您只能使用ColorDrawable .

行布局在 ListAdapter.getView() 中设置.

编辑

你的ListView应该是这样的:

<ListView
    android:id="@+id/android:list"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"    
    android:dividerHeight="0dp"
    android:listSelector="@drawable/list_selector">
</ListView>

为了设置列表行的样式,您需要创建 ListAdapter 。由于您使用的是自定义布局,因此您需要扩展 ArrayAdapter<?>BaseAdapter然后调用ListView.setListAdapter(ListAdapter)在您的ListView上。查看this article来自沃盖拉。

关于java - 设置 ListView 背景时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253804/

相关文章:

java - 当 CellSignalStrength 类没有构造函数时,如何扩展它?

java - 多个不同的 map 可以共享相同的键集吗?

eclipse - Android 模拟器 AVD 窗口大小

java - 如何与设备通信?

android - Android中使用AlarmManager更新App Widgets的效率

java - 在收到 UDP 数据包之前不显示 Activity

java - 修复 ImageLoader : "Try to initialize ImageLoader which had already been initialized before" 的警告

java - Eclipse - 如何运行 2 个 junit 测试

java - 如何以编程方式设置 adUnitId?

java - Camel仅在启动时发送消息,然后停止