android边框透明度,Android形状边框采用渐变和中间透明
时间: 2023-09-20 11:11:55 浏览: 46
要让 Android 边框具有渐变和中间透明度,您可以使用 GradientDrawable 类。下是一个示例代码,可以创建一个带有渐变和中间透明度的矩形边框:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF0000"
android:endColor="#00FF00"
android:type="linear"
android:angle="0"/>
<stroke
android:width="2dp"
android:color="@android:color/transparent" />
<corners android:radius="20dp" />
</shape>
```
在上面的代码中,我们使用了 GradientDrawable 类来创建一个矩形形状。我们使用 gradient 元素来定义渐变,使用 stroke 元素来定义边框的宽度和颜色。在 stroke 元素中,我们将颜色设置为透明,以便在边框的中间添加透明度。最后,我们使用 corners 元素来定义圆角的半径。
您可以将上面的 XML 代码保存为 drawable 文件,并将其应用于 Android 视图的背景或边框属性。例如,在 TextView 中使用它作为边框:
```xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="@drawable/my_border"/>
```
这将为 TextView 创建一个带有渐变和中间透明度的边框。