那么你想要实现的并不像那么简单。
步骤一:将分隔线设为透明,并稍微增加高度:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="8dp"/>
第二步:为了实现“小横线”效果,您可以将自定义可绘制项作为列表视图项的背景添加,比如列表视图项定义为“list_item.xml”:
Step Two: In order to achieve the 'little line' effect, you can add a custom drawable as the list view item background, say the list view item is defined as '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>
但那样将禁用“Holo Selector”效果,每当您在listview上单击或突出显示项目时,都会在其上绘制Holo Blue颜色,这就是为什么如果您注意到列表项背景,我们没有使用图层列表可绘制,而是使用名为“list_item_selector”的选择器。
以下是该选择器,当未被按下时,它使用图层列表可绘制,而当被按下时,则使用Holo-blue颜色:
<?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>
评论编辑
完全有可能,您可以为列表视图项定义一个固定的高度,但是建议设置一个最小高度,而不是预定义的永远不改变的高度。
假设这是我的列表项布局:
<?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>
<!
<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" >
这就是关于这个主题的全部内容,现在来居中文本,更简单了:
再强调一下:注意: 只有当您没有将高度设置为 wrap_content 时,这才起作用,否则它无关紧要。
android:gravity="center_vertical"
希望这能有所帮助。