nin*_*u19 0 android gradient colors kotlin
我想设置带有渐变的背景。这是我的代码:
val startColor = "0xFFAC235E"
val endColor = "0xFF640C35"
val gradient = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
intArrayOf(
startColor.toInt(),
endColor.toInt()
)
)
view.background = gradient
Run Code Online (Sandbox Code Playgroud)
并通过一个例外:
java.lang.NumberFormatException: For input string: "0xFFAC235E"
Run Code Online (Sandbox Code Playgroud)
如果我替换startColor = 0xFFAC235E,上面的代码可以正常工作。但这不是我想要的。
我需要将颜色作为参数字符串。无论如何可以转换它吗?
尝试用 # 替换 0x。
例如:
startColor.replace("0x", "#")
Run Code Online (Sandbox Code Playgroud)
通常我们用十六进制颜色代码定义颜色。所以,我认为这对你有用。
编辑
您必须解析颜色字符串以将其转换为整数。
Color.parseColor(startColor.replace("0x", "#"))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
234 次 |
最近记录: |