首页 > 解决方案 > 安卓三色渐变

问题描述

我想将我的 android 应用程序的背景设置为三色渐变。我尝试了以下代码行:

binding.splashScreenRelativeLayout.background = gradient

其中 binding 是我的 XML 布局的数据绑定变量,而渐变是

gradient = GradientDrawable(GradientDrawable.Orientation.BL_TR, intArrayOf(startColor, midColor, endColor)).

我还尝试创建一个自定义可绘制类并将其用作 XML 元素,但没有任何成功,因为我没有找到太多使用 XML 和自定义可绘制对象的文档

标签: androidxmluser-interfacekotlin

解决方案


您是否尝试过创建可绘制资源文件?您可以创建一个形状,并在其中设置一个带有开始、中心和结束颜色的渐变,如下所示:

    <shape
    xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="@color/colorPrimary"
        android:centerColor="@color/colorAccent"
        android:endColor="@color/colorPrimaryDark"/>

</shape>

然后你会有一个三色渐变。现在您要做的就是将其设置为布局文件的背景。


推荐阅读