【问题标题】:App Crash when setting ListView background设置 ListView 背景时应用程序崩溃
【发布时间】:2013-08-15 13:34:16
【问题描述】:

我正在尝试为我的应用创建一种卡片布局。在activity_events 下,我需要将list_row 设置为列表中每个项目的背景。如下所示,应用程序在调用 Activity 时崩溃。但是仅删除 ListView android:background="@layout/list_row" 会导致一切正常,只是看起来不正确。我已经尝试将 list_row 移动到 drawable 文件夹并使用 @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" />

【问题讨论】:

  • 发布异常报告
  • 我的回答对你有帮助吗?

标签: java android eclipse listview


【解决方案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&lt;?&gt;BaseAdapter,然后在您的 ListView 上调用 ListView.setListAdapter(ListAdapter)。查看 Vogella 的 this article

【讨论】:

  • 那么有没有办法在不制作庞然大物的情况下将布局嵌套在另一个布局中?我确实尝试将它用作可绘制对象,但它似乎不喜欢那样。 ' android:src ' 会是更好的选择吗?我实际上对布局知之甚少,对此非常陌生。
  • 请查看更新后的答案,并务必查看链接的文章。
【解决方案2】:

您不必在ListView 上放置背景,您需要设置一个适配器(扩展BaseAdapter)到ListView 并让它从getView() 返回您的布局(您可以使用@987654326 @class 来夸大你的R.layout.list_row)

编辑:更多信息here

【讨论】:

    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 2016-05-05
    • 2016-08-22
    相关资源
    最近更新 更多