ListView 分隔线不可见

发布于 2024-11-25 13:06:43 字数 1890 浏览 2 评论 0原文

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/样本/ApiDemos/src/com/example/android/apis/view/List14.html

A ListActivity uses an extension of BaseAdapter and the following layout:

    <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>

It inflates the following layout for the rows:

            <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>   

The drawable/divider.xml looks like this at the moment, but I've tried all different kinds:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
      <stroke android:color="#FF000000">
      </stroke>
    </shape>    

If I read the drawable from an image it works. Why doesn't it work when I define the drawable as a .xml file?

Update:
The code is following the same pattern as the EfficientAdapter in the Android Developer examples:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-12-02 13:06:43

下面的代码对我有用:

列表布局:

<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>

my_shape:

    <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>

Code below works for me:

list layout:

<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>

my_shape:

    <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>
假扮的天使 2024-12-02 13:06:43

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

<?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>

只需确保第二个形状的颜色与列表的背景和分隔线高度“同步”即可。

Here's my workaround for dashed divider:

<?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>

Just make sure to "sync" second shape's color with list's background and divider heights.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文