0

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

下边是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" />

4

2 回答 2

1

不能使用布局作为背景。您只能使用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<?>orBaseAdapter然后调用ListView.setListAdapter(ListAdapter)您的ListView. 查看Vogella 的这篇文章

于 2013-08-15T13:38:03.990 回答
0

您不必在 上放置背景ListView,您需要将适配器(扩展BaseAdapter)设置为ListView并让它返回您的布局getView()(您可以使用LayoutInflater类来膨胀您的R.layout.list_row

编辑:更多信息在这里

于 2013-08-15T13:44:25.470 回答