Sam*_*mmy 363 android android-layout android-listview
我尝试在listView上使用marginBottom来在listView Item之间创建空格,但仍然将这些项连接在一起.
它甚至可能吗?如果是,是否有特定的方法来做到这一点?
我的代码如下
<LinearLayout
android:id="@+id/alarm_occurences"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent"
android:background="#EEEEFF"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/occurences"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的自定义列表项:
<com.android.alarm.listItems.AlarmListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/alarm_item_background"
android:layout_marginBottom="10dp"
>
<CheckedTextView
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif"
android:padding="10dp"
/>
</com.android.alarm.listItems.AlarmListItem>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如何在列表项之间建立间距?
Nik*_*man 819
@Asahi几乎触手可及,但我只想为以后通过google浮动的人添加一些XML:
<ListView android:id="@+id/MyListView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"/>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,Android会拒绝诸如"10","10.0"和"10sp"之类的dividerHeight
值.它需要浮点数和单位,例如"10.0sp".正如@Goofyahead所说,您还可以使用与显示无关的像素作为此值(即"10dp").
idu*_*olz 43
虽然Nik Reiman的解决方案可以解决,但我发现它并不是我想做的最佳解决方案.使用分隔符设置边距的问题是分隔符将不再可见,因此您无法使用它来显示项目之间的清晰边界.此外,它不会为每个项目添加更多"可点击区域",因此如果您想让您的项目可点击并且您的项目很薄,那么任何人都很难点击某个项目,因为分隔符所添加的高度不是项目的一部分.
幸运的是,我找到了一个更好的解决方案,它允许你显示分隔线,并允许你使用非边距而不是填充来调整每个项目的高度.这是一个例子:
列表显示
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Run Code Online (Sandbox Code Playgroud)
项目清单
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Item"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Vas*_*asu 17
你应该将你的ListView
项目(比方说your_listview_item
)包装在其他布局中,例如LinearLayout
并添加边距your_listview_item
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<your_listview_item
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
...
...
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这样,您还可以根据需要在项目的右侧和左侧添加空间ListView
.
小智 11
我添加更多空间但保持水平线的解决方案是添加divider.xml
到res/drawable
文件夹中并定义内部的线形:
divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="1px"
android:color="@color/nice_blue" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后在我的列表中,我引用我的分隔符如下:
<ListView
android:id="@+id/listViewScheduledReminders"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginBottom="@dimen/mediumMargin"
android:layout_weight="1"
android:divider="@drawable/divider"
android:dividerHeight="16.0dp"
android:padding="@dimen/smallMargin" >
</ListView>
Run Code Online (Sandbox Code Playgroud)
注意android:dividerHeight="16.0dp"
通过增加和减少这个高度我基本上在分隔线的顶部和底部添加更多填充.
我使用此页面作为参考:http://developer.android.com/guide/topics/resources/drawable-resource.html#stroke-element
小智 8
如果你想显示一个带边距的分隔线并且没有拉伸它 - 使用InsetDrawable(大小必须是格式,关于哪个@Nik Reiman):
列表显示:
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="@drawable/separator_line"
android:dividerHeight="10.0px"/>
Run Code Online (Sandbox Code Playgroud)
@绘制/ separator_line:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="5.0px"
android:insetRight="5.0px"
android:insetTop="8.0px"
android:insetBottom="8.0px">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/colorStart"
android:centerColor="@color/colorCenter"
android:endColor="@color/colorEnd"
android:type="linear"
android:angle="0">
</gradient>
</shape>
</inset>
Run Code Online (Sandbox Code Playgroud)
您可以使用:
android:divider="@null"
android:dividerHeight="3dp"
Run Code Online (Sandbox Code Playgroud)
例:
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" android:layout_gravity="center_horizontal"
android:dividerHeight="3dp"
android:divider="@null" android:clickable="false"/>
Run Code Online (Sandbox Code Playgroud)
小智 6
对于我的应用程序,我已经这样做了
<ListView
android:id="@+id/staff_jobassigned_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="10dp">
</ListView>
Run Code Online (Sandbox Code Playgroud)
只是set the divider to null
并为分隔线提供高度对我有用。
例子 :
android:divider="@null"
Run Code Online (Sandbox Code Playgroud)
或者
android:divider="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)
这是结果
我意识到已经选择了一个答案,但我只想分享在我遇到这个问题时最终为我工作的内容.
我有一个listView,其中listView中的每个条目都由它自己的布局定义,类似于Sammy在他的问题中发布的内容.我尝试了改变分隔高度的建议方法,但即使使用不可见的分隔符,也不会看起来太漂亮了.经过一些实验,我只是android:paddingBottom="5dip"
在XML文件中的最后一个TextView布局元素中添加了一个定义单个listView条目的元素.
这最终给了我通过使用我想要实现的目标android:layout_marginBottom
.我发现这种解决方案比试图增加分隔高度产生更美观的结果.
归档时间: |
|
查看次数: |
213379 次 |
最近记录: |