[英]Why ListView doesnot show divider
對於這種特殊情況,我使用默認的listview布局,並使用ListFragment
擴展我的活動。 我在列表視圖中顯示分隔線時遇到問題。 我正在為我的listview行格式使用以下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
文件的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
看到數據,但沒有顯示分隔符,即背景為@drawable/shape
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>
或更簡單的方法是從除法中獲取listView並為其添加除法器
getListView().setDividerHeight(heightOfDivider);
getListView().setDivider(yourDrawable);
如果有任何問題請發表評論
祝好運 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.