ListView分割线不可见。

6
一个ListActivity使用BaseAdapter的扩展和以下布局:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical"     
        android:background="@color/application_background_color">

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:drawSelectorOnTop="false"
    android:divider="@drawable/divider"
    android:dividerHeight="2dip" android:clickable="true"
    android:choiceMode="singleChoice"    
    android:cacheColorHint="@color/application_background_color">
</ListView>

   </LinearLayout>

它为行填充以下布局:
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView android:id="@+id/Row" 
        android:layout_width="fill_parent" android:layout_height="50dip"
        android:textSize="20dip" android:textColor="#000000"
        android:layout_margin="8dip"
        android:gravity="center_vertical"></TextView>

    </LinearLayout>   

drawable/divider.xml目前看起来像这样,但我尝试了各种不同的方式:
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
      <stroke android:color="#FF000000">
      </stroke>
    </shape>    

如果我从图像中读取可绘制对象,它可以工作。为什么我将可绘制对象定义为.xml文件时不起作用?
更新: 代码遵循Android开发人员示例中EfficientAdapter的相同模式: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

也许问题出在你的适配器上。尝试在适配器的areAllItemsEnabled()方法中返回true。 - grine4ka
2个回答

2
以下代码适用于我:
列表布局:
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:cacheColorHint="@android:color/transparent"
    android:background="@android:color/transparent"
    android:textFilterEnabled="false"
    style="@style/ListDivider"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ListView>

styles.xml

<resources>
    <style name="ListDivider">
        <item name="android:divider">@drawable/my_shape</item>
        <item name="android:dividerHeight">5dp</item>
    </style>
</resources>

我的形状:

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <padding
            android:top="2dp"
            android:left="2dp"
            android:right="2dp"
            android:bottom="2dp" />
        <corners
            android:radius="5dp" />
        <solid
            android:color="@android:color/white" />
    </shape>

谢谢,但它在我的代码中没有起作用。这意味着可能是代码中的某些问题导致了这个问题,而不是xml文件。 - Vanja

0

这是我解决虚线分隔符的方法:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="1dip">
        <shape android:shape="rectangle" >
            <solid android:color="#FF123243" />

            <stroke
                android:dashGap="5dp"
                android:dashWidth="8dp"
                android:width="1dp"
                android:color="#FFFFFFFF" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#FF000000" />
        </shape>
    </item>
</layer-list>

请确保将第二个形状的颜色与列表的背景和分隔符高度“同步”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接