在安卓视图中如何为顶部和底部边缘添加不同颜色的边框

3

我有一个TextView,我想在它的顶部和底部边缘上添加不同颜色的边框。我知道如果要添加单一颜色的边框,我们可以使用以下代码:

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

<item
    android:top="1dp" android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
    </shape>
</item>

但如果我们需要不同颜色的边,该怎么做呢?

1个回答

9

您已经非常接近您想要的东西了,您需要做的是在默认项目下方添加另一个项目。这两个项目是您的顶部/底部边框。通过将底部/顶部1dp添加到两者中,您可以显示出两种颜色。

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

<item
    android:top="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000"/>
    </shape>
</item>

<item
    android:top="1dp" android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
    </shape>
</item>
</layer-list>

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