在android中创建渐变边框?

时间:2012-08-23 18:54:32

标签: android border

如何创建渐变边框,如下图所示?

border

1 个答案:

答案 0 :(得分:7)

您可以通过使用图层列表并弄乱填充来实现此目的。你需要3个元素:

1:border.xml形状,它只是边框颜色的实心形状:border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ff0000"/>
</shape>

2:'内部'形状,您希望边框出现的形状:inner.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00ff00"/>
</shape>

3:一个图层列表,它将这两个放在彼此之上。您可以通过在内部形状上设置填充来创建边框:layerlist.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/border"/>
<item android:drawable="@drawable/inner"

在此指定您想要中风的位置(上,左,右,下)

android:top="3dp" android:right="0dp" android:bottom="3dp"
android:left="3dp" />

将其设置为TextView,Layout等的背景(您希望显示笔划的位置)

或者只是创建带有边框的9Patch图片。