为什么 ListView 不显示分隔符
Why ListView doesnot show divider
对于这种特殊情况,我使用默认的列表视图布局,并且我正在使用 ListFragment
扩展我的 activity。我在列表视图中显示分隔符时遇到问题。我正在为我的列表视图行格式使用以下 xml 文件。
<?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"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/windowBackground"
android:padding="2dp" >
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<TextView android:id="@+id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium" />
<TextView android:id="@+id/tvdescription"
android:layout_below="@id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small" />
</RelativeLayout>
<!-- This is Supposed to be the divider -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape" >
</RelativeLayout>
</RelativeLayout>
使用 shape.xml
文件的倒数第二个 RelativeLayout
应该是分隔符。 Shape.xml
文件如下
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="2px" />
<stroke
android:width="2px"
android:color="@color/colorAccent" />
<solid android:color="@color/colorPrimary"/>
</shape>
列表数据输出正常。我可以在两个 TextViews
中看到数据,但它没有显示背景为 RelativeLayout
的分隔符 @drawable/shape
是的,您可以像以前一样在布局行中放置一个分隔线,但您必须更正它
<?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"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/windowBackground"
android:padding="2dp" >
<RelativeLayout
android:id="@+id/row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<TextView android:id="@+id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium" />
<TextView android:id="@+id/tvdescription"
android:layout_below="@id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small" />
</RelativeLayout>
<!-- This is Supposed to be the divider -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/row_id"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@color/yourColor" >
</RelativeLayout>
</RelativeLayout>
或更简单的方法是从分界线中获取 listView 并在其上放置一个分界线
getListView().setDividerHeight(heightOfDivider);
getListView().setDivider(yourDrawable);
如有问题请留言
祝你好运!
对于这种特殊情况,我使用默认的列表视图布局,并且我正在使用 ListFragment
扩展我的 activity。我在列表视图中显示分隔符时遇到问题。我正在为我的列表视图行格式使用以下 xml 文件。
<?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"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/windowBackground"
android:padding="2dp" >
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<TextView android:id="@+id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium" />
<TextView android:id="@+id/tvdescription"
android:layout_below="@id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small" />
</RelativeLayout>
<!-- This is Supposed to be the divider -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape" >
</RelativeLayout>
</RelativeLayout>
使用 shape.xml
文件的倒数第二个 RelativeLayout
应该是分隔符。 Shape.xml
文件如下
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="2px" />
<stroke
android:width="2px"
android:color="@color/colorAccent" />
<solid android:color="@color/colorPrimary"/>
</shape>
列表数据输出正常。我可以在两个 TextViews
中看到数据,但它没有显示背景为 RelativeLayout
的分隔符 @drawable/shape
是的,您可以像以前一样在布局行中放置一个分隔线,但您必须更正它
<?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"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/windowBackground"
android:padding="2dp" >
<RelativeLayout
android:id="@+id/row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<TextView android:id="@+id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium" />
<TextView android:id="@+id/tvdescription"
android:layout_below="@id/tvtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small" />
</RelativeLayout>
<!-- This is Supposed to be the divider -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/row_id"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@color/yourColor" >
</RelativeLayout>
</RelativeLayout>
或更简单的方法是从分界线中获取 listView 并在其上放置一个分界线
getListView().setDividerHeight(heightOfDivider);
getListView().setDivider(yourDrawable);
如有问题请留言
祝你好运!