在android视图的顶部和底部边缘添加不同颜色边框的方法

Yog*_*rle 3 xml user-interface android android-layout

我有一个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>
Run Code Online (Sandbox Code Playgroud)

但是如果我们需要不同颜色的边缘,需要做些什么呢?

Eva*_*hir 7

你非常接近你想要的,你需要做的是在你的默认项目下添加另一个项目.这两个项目是你的上/下边框.通过向两者添加bottom/top 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>
Run Code Online (Sandbox Code Playgroud)