gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:14:22 25 4
gpt4 key购买 nike

我正在尝试为我的应用程序创建一种卡片布局。在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com