简体   繁体   English

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

[英]Way to add a border of different color to top and bottom edge of an android view

I have a TextView and I'd like to add border with different colors along its top and bottom edges. 我有一个TextView,我想在其顶部和底部边缘添加不同颜色的边框。 I know that inorder to add a border of one color along all edges we can simply use following code: 我知道为了沿所有边缘添加一种颜色的边框,我们可以简单地使用以下代码:

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

But what needs to be done if we require edges with different colors? 但是如果我们需要不同颜色的边缘,需要做些什么呢?

You're quite close to what you want, what you need to do is add another item below your default item. 你非常接近你想要的,你需要做的是在你的默认项目下面添加另一个项目。 Those two items are your top/bottom borders. 这两个项目是你的上/下边框。 By add bottom/top 1dp to both, you reveal the two colours. 通过向两者添加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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM