我想在 ListView 中的每个项目之间添加填充,但我想保留默认分隔符,因为我认为它在美学上令人愉悦。任何更宽的东西看起来都很丑。
我目前有:
<com.example.practice.MyListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:layout_below="@id/name" />
现在,我尝试使用透明分隔线,这成功地获得了我想要的间距,但后来我看不到小线。如果我不使用透明的分隔线,我就会看到一条又粗又丑的线。我想保留显示的默认行,并在每个 ListView 项的顶部添加一些间距。
最佳答案
那时您将无法像那样简单地实现您想要的目标。
第一步:将分隔线设置为透明,并将高度调大一点:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="8dp"/>
第二步:为了实现'小线条'的效果,你可以添加一个自定义的drawable作为list view item的背景,假设list view item定义为'list_item.xml':
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<-- Add a Custom background to the parent container of the listview items -->
android:background="@drawable/list_item_selector"
android:orientation="vertical" >
<-- Rest of the item layout -->
</LinearLayout>
当然,那个项目可以是任何你喜欢的东西,我的是:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/bg_gray" />
<solid android:color="@android:color/white" />
</shape>
</item>
<item android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#FFDDDDDD" />
<solid android:color="#00000000" />
</shape>
</item>
</layer-list>
但这会禁用“全息选择器”效果,每当您单击或突出显示 ListView 上的项目时,都会在其上绘制全息蓝色,这就是为什么如果您注意到列表项背景我们没有我们不使用图层列表可绘制对象,而是使用名为“list_item_selector”的选择器。
这是选择器,未按下时使用图层列表可绘制,按下时使用全息蓝色:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="@drawable/list_item_bg2"
/>
<item android:state_pressed="true"
android:drawable="@color/holo_blue"
/>
</selector>
编辑评论
绝对可以,您可以为 ListView 项定义一个设置高度,但是,建议设置一个最小高度,而不是一个永远不会改变的预定义高度。
假设这是我的列表项布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/grid_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_toRightOf="@+id/grid_image"
android:minHeight="48dp"
android:padding="8dp"
android:textIsSelectable="false" />
</RelativeLayout>
所有需要的是,
第一步:根据需要在 values/文件夹中的 dimens.xml 文件中定义最小高度或最大高度。为什么?因为高度肯定会根据布局而变化,并且您可以为每个设备密度定义不同的 dimens.xml。
在 dimens.xml 中,说:
<resources>
<!-- List Item Max and Min Height -->
<dimen name="list_item_min_height">48dp</dimen>
<dimen name="list_item_max_height">96dp</dimen>
<dimen name="list_item_set_height">64dp</dimen>
</resources>
然后为列表项布局的父 LinearLayout
使用任何值。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_min_height" >
主题就这些了,现在让文本居中,更简单:
如果您使用的是 TextView 并被包装到 RelativeLayout 中,请使用:
android:layout_centerVertical="true"
如果您使用的是 LinearLayout,请使用:
android:layout_gravity="center_vertical"
并将其与:注意 这仅在您未将高度设置为 wrap_content 时才有效,否则它是无关紧要的。
android:gravity="center_vertical"
希望对您有所帮助。
关于android - 如何在android listview中的项目之间添加空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556106/