5

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

边界

4

1 回答 1

8

您可以通过使用图层列表并弄乱填充来实现此目的。你需要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图像。

于 2012-08-23T20:20:19.353 回答