Android 为什么ListView不显示分隔符

Android 为什么ListView不显示分隔符,android,android-layout,listview,android-listview,Android,Android Layout,Listview,Android Listview,对于这种特殊情况,我使用默认的listview布局,并使用ListFragment扩展我的活动。我在listview中显示分隔符时遇到问题。我将以下xml文件用于我的listview行格式 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pare

对于这种特殊情况,我使用默认的listview布局,并使用
ListFragment
扩展我的活动。我在listview中显示分隔符时遇到问题。我将以下xml文件用于我的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"
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>

列表数据输出正常。我可以在两个
文本视图中看到数据
,但它没有显示分隔符,而分隔符是
相对长度
,背景为
@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>
如果有任何问题,请留言


祝你好运

没错,你可以像以前一样在布局行中放置分隔符,但你必须纠正它

<?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>
如果有任何问题,请留言


祝你好运

为什么要使用shape.xml制作线条您可以使用
查看
而不是
相对布局
只需在
android:layout\u height=“2dp”中指定高度
@Clarivoyant我知道我可能会使用该解决方案,但我正试图使用这个shape.xml文件。请记住,您可以自行为ListView设置分隔符,而不是将其添加到每个项目中。@McAdam331您的意思是调用setDivider(Drawable Drawable)函数?或者在XML中,只要有ListView标记,就可以执行这些操作,这就是我习惯做的事情。如果您愿意,您也可以通过编程实现。为什么要使用shape.xml制作线条您可以使用
view
而不是
relative layout
只需在
android:layout\u height=“2dp”中指定高度
@Clarivoyant我知道我可能会使用该解决方案,但我正试图使用这个shape.xml文件。请记住,您可以自行为ListView设置分隔符,而不是将其添加到每个项目中。@McAdam331您的意思是调用setDivider(Drawable Drawable)函数?或者在XML中,只要有ListView标记,就可以执行这些操作,这就是我习惯做的事情。如果你愿意的话,你也可以通过编程来实现。我知道这个解决方案,我会接受你的答案。这与另一个人在第一条评论中提出的解决方案相同,但主要问题是为什么我的布局没有绘制shape.xml中定义的形状?您的形状没有显示,因为可能是因为您在行中隐藏了它,因为您确实在下方放置了“layout_”,请尝试使用我放置的代码并替换“@color/yourColor”我知道这个解决办法,我会接受你的回答。这与另一个人在第一条评论中提出的解决方案相同,但主要问题是为什么我的布局没有绘制shape.xml中定义的形状?您的形状没有显示,因为可能是因为您在行中隐藏了它,因为您确实在下方放置了“layout_”,请尝试使用我放置的代码并替换“@color/yourColor”根据你的身材
getListView().setDividerHeight(heightOfDivider);
getListView().setDivider(yourDrawable);