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

问题描述:

我有一个TextView,并且我想沿着它的顶部和底部边缘添加具有不同颜色的边框。我知道,序沿着所有的边缘,我们可以简单地使用下面的代码添加一种颜色的边框:向Android视图的顶部和底部边缘添加不同颜色边框的方法

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

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

你非常接近你想要的,你需要做的是在你的默认项目下添加另一个项目。这两个项目是你的顶部/底部边界。通过向两者添加底部/顶部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> 
+0

谢谢你工作得很好:) –