我目前有:
<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_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”效果,无论何时单击或突出显示列表视图中的项目,都会有一个Holo Blue颜色,这就是为什么如果你注意到列表项背景我们没有使用一个图层列表drawable,我们使用了一个名为’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>
编辑评论
绝对可以,您可以为列表视图项定义设置高度,但是,建议设置最小高度,而不是永远不会更改的预定义高度.
说这是我的列表项布局:
<?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"
希望有所帮助.