簡體   English   中英

如何在android listview中的項目之間添加空間?

[英]How to add space between items in android listview?

我想在列表視圖中的每個項目之間添加填充,但我想保留默認分隔符,因為我認為它在美學上令人愉悅。 任何更寬的東西看起來都很丑陋。

我目前有:

<com.example.practice.MyListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:layout_below="@id/name" />

現在,我嘗試使用透明分隔線,這成功地獲得了我想要的間距,但后來我看不到小線。 如果我不使用透明分隔線,我就會有一條粗粗的丑陋線條。 我想保留顯示的默認行,只需在每個列表視圖項的頂部添加一些間距。

你將無法像那樣簡單地實現你想要的。

第一步:將分隔線設置為透明,並將高度調大一點:

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/transparent"
    android:dividerHeight="8dp"/>

第二步:為了實現“小線條”的效果,你可以添加一個自定義drawable作為列表視圖項的背景,比如列表視圖項定義為“list_item.xml”:

<?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"
<-- Add a Custom background to the parent container of the listview items -->
android:background="@drawable/list_item_selector"
android:orientation="vertical" >
<-- Rest of the item layout -->
</LinearLayout>

當然,那個項目可以是你喜歡的任何東西,我的是:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
  <shape 
    android:shape="rectangle">
        <stroke android:width="1dp" android:color="@color/bg_gray" />
        <solid android:color="@android:color/white" />
    </shape>
</item>
<item android:bottom="1dp"> 
    <shape 
        android:shape="rectangle">
        <stroke android:width="1dp" android:color="#FFDDDDDD" />
        <solid android:color="#00000000" />
    </shape>
</item>
</layer-list>

但這會禁用“全息選擇器”效果,無論何時單擊或突出顯示列表視圖上的項目,都會在其上繪制全息藍色,這就是為什么如果您注意到我們沒有使用的列表項背景圖層列表可繪制,我們使用了一個名為“list_item_selector”的選擇器。

這是選擇器,它在未按下時使用可繪制的圖層列表,並在按下時使用 Holo-blue 顏色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:state_pressed="false"
    android:drawable="@drawable/list_item_bg2"        
    />
 <item android:state_pressed="true"
    android:drawable="@color/holo_blue"
    />
</selector>

編輯評論

絕對有可能,您可以為列表視圖項定義一個設置高度,但是,建議設置一個最小高度,而不是一個永遠不會改變的預定義高度。

說這是我的列表項布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/grid_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/grid_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:gravity="center_vertical"
    android:layout_toRightOf="@+id/grid_image"
    android:minHeight="48dp"
    android:padding="8dp"
    android:textIsSelectable="false" />

</RelativeLayout>

所有需要的是,

第一步:根據您的喜好,在 values/ 文件夾中包含的 dimens.xml 文件中定義最小高度或最大高度。 為什么? 因為高度肯定會根據布局而變化,並且您可以為每個設備密度定義不同的 dimens.xml。

在 dimens.xml 中,說:

<resources>

<!-- List Item Max and Min Height -->

    <dimen name="list_item_min_height">48dp</dimen>
    <dimen name="list_item_max_height">96dp</dimen>
    <dimen name="list_item_set_height">64dp</dimen>

</resources>

然后使用列表項布局的父LinearLayout的任何值。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_item_min_height" >

這就是該主題的內容,現在將文本居中,甚至更簡單:

  • 如果您使用的是 TextView 並被包裝成一個 RelativeLayout,請使用: android:layout_centerVertical="true"

  • 如果您使用的是 LinearLayout,請使用: android:layout_gravity="center_vertical"

並將其與:注意這僅在您未將高度設置為 wrap_content 時才有效,否則無關緊要。

  android:gravity="center_vertical"

希望有幫助。

我不知道我是否准確理解你的問題。 如果您希望分隔線是透明的,那么您會看到每個ListView之間的背景平靜,因此它在滾動時會產生一種 3D 效果。 你可以這樣做:

在您的 list_item xml 中,為LinearLayout提供您想要的背景顏色,例如:

<LinearLayout 
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="match_parent"    
android:padding="4dip"
android:orientation="horizontal"
android:background="#FFFFFF"
>

然后給你的ListView一個像這樣的背景顏色:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentListView"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="8dip"
android:background="#0000FF"
android:cacheColorHint="@android:color/transparent"
android:drawSelectorOnTop="true"
/>

現在你的ListView在你的背景上滾動

我希望這就是你想要的。

增加列表項之間間距的另一種方法是通過提供具有所需間距的 layout_height 屬性,向適配器代碼添加一個空視圖。 例如,為了增加列表項之間的底部間距,將此虛擬視圖(空視圖)添加到列表項的末尾。

<View
android:layout_width="match_parent"
android:layout_height="15dp"/>

因此,這將在列表視圖項之間提供 15 dp 的底部間距。 如果父布局是 LinearLayout 並且方向是垂直的,您可以直接添加它,或者對其他布局采取適當的步驟。 希望這可以幫助 :-)

你可以簡單地使用分隔線

看下面的例子

  <ListView
    android:id="@+id/activity_main_listview_data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/transparent"
    android:dividerHeight="10dp"
    />

在這里,在 android:divider 中,您可以設置顏色或使其透明,並在dividerHeight 中添加項目之間的空間。

對於那些希望分隔線可見但仍想增加更多空間的人來說,這是一個解決方案。 要完全擺脫分隔符,請將其設置為 @null 並將分隔符高度設置為 0dp。 這是地雷的通用 ListView。

<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:layout_alignLeft="@+id/txtMake"
    android:layout_below="@+id/txtMake"
    android:fadeScrollbars="false"
    android:scrollbarThumbVertical="@drawable/scroll_bar_color"
    android:scrollbarStyle="outsideInset" >
</ListView>

接下來,轉到使用適配器填充列表視圖的 xml 文件。 轉到您的容器(Example RelativeLayout...)並簡單地添加以下內容。

    android:layout_marginTop="15dp"

這實際上會為那些不使用分隔線的人增加空間。 與僅增加框大小的填充不同,這將增加每個項目之間的距離。

在 XMLfile 的ListView標簽內添加一個dividerHeight標簽並給它一個值(你想要的列表項之間的間距)。

它會在列表項之間為您提供合適的空間。

代碼:

<ListView
    android:id="@+id/listid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:divider="@drawable/divi"
    android:dividerHeight="60dp"></ListView>

現在創建一個可繪制的 XML 文件(在本例中名稱為 divi)。 在它里面添加一個stroke標簽並給它一個寬度,就可以了。

代碼:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
        android:color="#000000"
         />
</shape>
android:layout_width="match_parent"
android:textColor="@color/colorPrimary"
android:textSize="30dp"
android:layout_height="wrap_content"

完整代碼在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM