如何设置自定义ListView分隔线的宽度,使其比行宽更小?
现在通常使用RecyclerView
,而不是ListView
。查看这个问答了解如何设置RecyclerView
中分隔线的宽度。
<inset>
drawable/list_divider.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="10dp"
android:insetRight="10dp" >
<shape android:shape="rectangle" >
<solid android:color="@color/list_divider_color" />
</shape>
</inset>
在你的布局中:
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="@drawable/list_divider"
android:dividerHeight="1dp" >
</ListView>
Sources:
制作一个9 patch png,左右有透明像素。例如,一个53x4的.9.png,它的两侧各有25个透明像素(加上像素以进行9 patch),可以将1个像素拉伸,使得其两侧各有25个像素。
如果您不想制作9 patch图,那么您可以在list_item的xml代码中插入:
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="4dp"
android:background="#33B5E5" />
这将创建一条蓝色线,并且您可以轻松地控制该行的宽度。 要使此操作成功,您需要禁用listview的分隔符。具体方法请参见这里。
width
设为 match_parent
,然后设置左侧或右侧边距。 - Alaa M.你应该能够调用
mListView.setDivider(Drawable d);
并传递一个可包含在你的res/drawable文件夹中的drawable对象。如果想要使其几乎覆盖整个屏幕宽度,你可以创建一个包含水平线且左右两侧透明度越高越好的9-patch图片,并设置拉伸线的中间部分。
ListView
弃用的任何提及。我理解你这些话背后的想法,但我认为它可能会让某些人感到困惑。尽管如此,回答非常好。 - Viacheslav