开发者

Distance between items in a Listview

I have a ListView in my Layout.

    <ListView 
        android:id="@+id/list_infoitems"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
开发者_StackOverflow

Every list item layout looks like:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/selector_custombutton"
    android:padding="10dp"
    android:layout_gravity="center">
    <TextView
        android:id="@+id/text_history_station_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|left"
        android:textColor="@color/rnv_blue_540c"/>
    <TextView
        android:id="@+id/text_history_line_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|left"
        android:textColor="@color/rnv_blue_540c"/>      
</LinearLayout>

I want to have a distance between all item. For that I'v played with android:margin property, but without success.

Does anybody know, how to change the distance between item in a listview.

Thank you,

Mur


You could always set the dividerHeight property. Android Docs

<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:dividerHeight="5dp"
/>

If that isn't what you want to do, in your listview item you could wrap your LinearLayout in a RelativeLayout and add a margin to the LinearLayout

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
    />
</RelativeLayout>


I have found that the ListView divider has fragmentation issues. In 2.3 devices, a grey line is implemented by default that doesn't show up on 4.x devices. This can be addressed by manually setting android:divider="some_value". I tend to handle all ListView item layout issues within the items layouts themselves. Its a dirty little trick, but resolves cross version issues. Note the addition of a "hidden" view below. This not only fixes the problem between items, but gives you equal padding after the last item in your list.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="5dp" />

    <View
        android:below="@id/list"
        android:layout_height="0dp"
        android:layout_width="0dp"
        android:margin_top="5dp" />
</RelativeLayout>


Use padding or Margin or the ListView itself.

<ListView 
        android:id="@+id/list_infoitems"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
/>


To encrease the distance between the items them self you can use

android:dividerHeight="xxdpi"

In

<ListView 
    android:id="@+id/list_infoitems"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="xxdpi"/>

Regarts, Marco

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜