[英]App Crash when setting ListView background
我正在嘗試為我的應用程序創建一種卡片布局。 在activity_events下,我需要將list_row設置為列表中每個項目的背景。 如下所示,應用程序在調用活動時崩潰。 但是僅刪除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" />
不能將布局用作背景。 您只能使用Color
或Drawable
。
行布局在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
上調用ListView.setListAdapter(ListAdapter)
。 查看Vogella的這篇文章 。
你不必把背景上ListView
,你需要設置一個適配器(擴大BaseAdapter
)的ListView
,並使其返回從布局getView()
你可以使用LayoutInflater
類虛增您R.layout.list_row
)
編輯 :更多信息在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.