Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

android - How to assign padding to Listview item divider line

enter image description here

How can i give padding to list item as i show in image. I want to make the divider in the layout as shown in the image.

this is my list fragment code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">

 <ListView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
     android:id="@+id/listV_main"
    android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>

This is my List section code

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<include
    android:id="@+id/list_item_section_text"
    layout="@android:layout/preference_category"
     />

This is my list item code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:adjustViewBounds="true"
android:paddingRight="?android:attr/scrollbarSize"
   >
<ImageView
    android:id="@+id/showlist_item_entry_drawable"
    android:layout_width="82dp"
    android:adjustViewBounds="true"
    android:layout_height="68dp"
    android:scaleType="fitXY"
    android:paddingLeft="9dp"/> 
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="6dip"
    android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip"
    android:layout_weight="1"
>
    <TextView android:id="@+id/list_item_entry_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal" /> 
    <TextView android:id="@+id/list_item_entry_summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/list_item_entry_title"
        android:layout_alignLeft="@id/list_item_entry_title"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:singleLine="true"
        android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use 'inset'.....

(list_divider.xml)

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="50dp"
    android:insetRight="50dp" >

 <shape>
    <solid android:color="@color/orange" />
    <corners android:radius="2.0dip" />
</shape>

</inset>

and in your list view add like this...

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider"
    ...
/>

you can set the inset value as desired...

UPDATE

As pointed out by @Giulio Piancastelli , If the background of list container is different from background of list item then you may use 'layer-list'...

(list_divider.xml)

  <?xml version="1.0" encoding="utf-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
   <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/list_background" />
        </shape>
    </item>


    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/divider_color"/>
        </shape>
    </item>

        </layer-list>

and in your list view add like this...

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider"
    ...
/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...