Android 列表视图分隔边距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11096304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listview divider margin
提问by Fabian
I'm trying to set a margin to a listview
divider.
The divider is a dashed line:
我正在尝试为listview
分隔符设置边距。
分隔线是一条虚线:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:dashGap="1dp"
android:dashWidth="1.5dp"
android:width="1dp"
android:color="#FF404040" />
<size android:height="3dp" />
</shape>
and a listview
where i set the divider
和listview
我设置分隔线的地方
<ListView
android:id="@+id/lv_news_feed_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:divider="@drawable/ch_dashed_line_divider"
/>
but I want a divider margin left and right.
I also tried to set a padding to the shape, but the listview
is ignoring the padding.
但我想分频器左边距和右边。我还尝试为形状设置填充,但listview
忽略了填充。
<padding
android:bottom="15dp"
android:left="15dp"
android:right="15dp"
android:top="15dp" />
Is there any possibility to set a margin to the listview
divider - except in the getView() of the Adapter?
是否有可能为listview
分隔符设置边距- 除了在适配器的 getView() 中?
回答by Wayne
Inset is the way to go
插入是要走的路
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="15dp"
android:insetRight="15dp" >
<shape
android:shape="line" >
<stroke
android:dashGap="1dp"
android:dashWidth="1.5dp"
android:width="1dp"
android:color="#FF404040" />
<size android:height="3dp" />
</shape>
</inset>
回答by ASP
Use 'inset'.....
使用“插入”.....
(list_divider.xml)
(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...
您可以根据需要设置插入值...
回答by need1milliondollars
You can use the following idea:
您可以使用以下想法:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/table_background"/>
</shape>
</item>
<item android:left="2dp" android:right="2dp">
... your shape here ...
</item> </layer-list>
It works for me. Hope it will help.
这个对我有用。希望它会有所帮助。
回答by Pradeep
U can use Gradient to get Right and Left margin instead of stroke.. Try this sample it starts and ends with black, in centre u'll get White.. It appears like u've given margin
你可以使用渐变来获得左右边距而不是描边..试试这个样本,它以黑色开始和结束,在中心你会得到白色......看起来你已经给了边距
<gradient android:startColor="#000000" android:centerColor="#ffffff"
android:endColor="#000000" />
回答by DecodeGnome
I don't believe it is possible :/ Although if you add your own drawable at the bottom of each list item and remove the ListViews divider you can customize it however you want :)
我不相信这是可能的:/尽管如果您在每个列表项的底部添加自己的 drawable 并删除 ListViews 分隔符,您可以根据需要对其进行自定义:)
Create the folder res/drawable and create a new xml:
创建文件夹 res/drawable 并创建一个新的 xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#666666"></solid>
</shape>
..Then use that drawable in you list item.
..然后在你的列表项中使用那个drawable。
Or a really quick and dirty way would ofc to create a thin LinearLayout (or some other layout) with colored background at the bottom of the list item to fake a customizable ListView divider..
或者一种非常快速和肮脏的方法是在列表项的底部创建一个带有彩色背景的薄 LinearLayout(或其他一些布局),以伪造一个可自定义的 ListView 分隔线..
Not a proper sollution, just some fixit ideas ;)
不是一个合适的解决方案,只是一些固定的想法;)