【问题标题】:Two-lined divider becomes invisible if height is lesser then 4dp如果高度小于 4dp,两行分隔线将变得不可见
【发布时间】:2013-06-03 20:18:32
【问题描述】:

我想要一个由两条平行线组成的分隔视图——白色和灰色。我在 SO 上找到了 xml:

<?xml version="1.0" encoding="utf-8"?>

<item android:bottom="1dp">
    <shape android:shape="line" >
        <stroke
            android:width="2dp"
            android:color="@android:color/darker_gray" />

        <size android:height="1dp" />
    </shape>
</item>
<item android:top="1dp">
    <shape android:shape="line" >
        <stroke
            android:width="2dp"
            android:color="@android:color/white" />

        <size android:height="1dp" />
    </shape>
</item>

然后我就这样使用它:

<View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="@drawable/divider" />

但它没有显示任何内容。我发现只有当我将高度设置为 4dp 或更高时,我的视图才可见。但这不是我想要的。我做错了什么?

附:好吧,我可以使用带有 background="@android:color/white 或 darker_grey" 的两个视图,但我希望两个视图只使用一个 drawable 就可以达到相同的效果。

【问题讨论】:

    标签: android android-layout android-drawable


    【解决方案1】:

    将形状更改为矩形对我有用:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:top="1dp">
             <shape android:shape="rectangle" >
                <solid android:color="@android:color/darker_gray" />
                <size android:height="1dp" />
            </shape>
        </item>
        <item android:bottom="1dp">
            <shape android:shape="rectangle" >
                <solid android:color="@android:color/white" />
                <size android:height="1dp" />
            </shape>
        </item>
    </layer-list>
    

    【讨论】:

    • 不工作。另请注意,我没有将此视图用作 listDivider。它只是另一个布局的一部分。
    • 同一父级中的所有其他视图都有 layout_height="wrap_content"。但是为什么它在 layout_height="4dp" 或更大时可见?
    • 嗨 - 将其更改为矩形对我有用。也许有 2 行和一个图层列表和填充,我不确定。但我可以在 2dp 的视图中设置它。我已经编辑了我的答案。
    【解决方案2】:

    试试这个-

             <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#ffffff" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多