我試圖創建一個簡單的列表視圖,每行都有一些填充。Android列表視圖行邊距
我是以一種非常簡單的方式完成的,但我不確定這是否是正確的方法。
這就是我得到的(代碼如下)。正如你可以看到當你點擊一排,它完全凸顯,我想(如果可能),僅強調裏面的視圖(帶有灰色背景,這是一個TextView)
這是代碼的活動佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:dividerHeight="0dp" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_events" />
</LinearLayout>
,這是列表項佈局的代碼:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#ccc"
android:text="placeholder" />
</FrameLayout>
感謝您的答覆!
PS。我沒有上傳Activity,它是一個簡單的ListActivity。
UPDATE
解決(感謝豪爾赫咀)。新的代碼是這樣的:
列表項:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="@drawable/event_focused"
android:text="placeholder" />
</FrameLayout>
事件集中文件(這是在繪製目錄):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@android:color/black" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@android:color/darker_gray" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@android:color/darker_gray" /> <!-- hovered -->
<item android:drawable="@android:color/darker_gray" /> <!-- default -->
</selector>
我還添加了這一點:
android:listSelector="@android:color/transparent"
我的ListView :)
您是否嘗試將邊距或填充設置爲ListView而不是? – yugidroid 2012-08-01 23:32:26
是的,但我得到了列表視圖中的滾動條:http://i.imgur.com/PsW5I.png,我不喜歡它:) – patrick 2012-08-01 23:45:11
我想你可以設置頂部和底部邊距到ListView和右邊並留下一個列表查看項目(FrameLayout) – yugidroid 2012-08-01 23:47:39