Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Kotlin或Java更改可绘制图形的笔划颜色_Java_Android_Kotlin_Drawable - Fatal编程技术网

如何使用Kotlin或Java更改可绘制图形的笔划颜色

如何使用Kotlin或Java更改可绘制图形的笔划颜色,java,android,kotlin,drawable,Java,Android,Kotlin,Drawable,我有一个可绘制的对象,我用它来设置线性布局的背景,用半透明的橙色线条将线性与圆形分开。但是在代码中的某个时候,我需要将这个背景(可绘制对象)的颜色更改为一个颜色,我只将其作为一个参数,而在我的颜色文件的颜色中没有它。我需要将此drawable的笔划颜色更改为运行时变量之一中的颜色 bg_static_show_password.xml 线性布局 我尝试使用的方法来改变颜色 fun showAlternativeForAnimation(view: LinearLayout) {

我有一个可绘制的对象,我用它来设置线性布局的背景,用半透明的橙色线条将线性与圆形分开。但是在代码中的某个时候,我需要将这个背景(可绘制对象)的颜色更改为一个颜色,我只将其作为一个参数,而在我的颜色文件的颜色中没有它。我需要将此drawable的笔划颜色更改为运行时变量之一中的颜色

bg_static_show_password.xml


线性布局


我尝试使用的方法来改变颜色

fun showAlternativeForAnimation(view: LinearLayout) {
        val drawable = view.background as GradientDrawable
        val theme = PasswordRecoveryTheme(ApplicationSession.instance?.themeId)
        drawable.setStroke(1, theme.getAccentColor(ApplicationFactory.context!!))

    }
该方法的参数为线性布局

当我尝试时,会出现以下异常: kotlin.TypeCastException:null不能强制转换为非null类型android.graphics.drawable.GradientDrawable

进行安全强制转换(
as?
),确保传递的视图的背景设置为shape drawable,并将参数更改为
view:view
,以便允许使用任何视图(LinearLayout、ConstraintLayout等)


您说您正在尝试更改线性布局的背景,但实际上您正在使用ConstraintLayout。这真的是你的意思吗?实际上是约束布局和父视图,在它里面我有一个线性布局,背景我想改变,在这种情况下,约束并不重要,因为它可以是任何布局,在这种情况下,事实是线性布局在另一个布局内Hanks Chrisvin,但是在这种情况下,只有我改变这个可绘制的颜色的问题没有得到解决,它只是避免了NullPointerException它给了你一个null,因为你可能传递了布局(没有形状可绘制作为背景)作为参数。真的,我的布局出了问题,非常感谢你打开我的眼睛
fun showAlternativeForAnimation(view: View) {
    val drawable = view.background as? GradientDrawable
    val theme = PasswordRecoveryTheme(ApplicationSession.instance?.themeId)
    drawable?.setStroke(1, theme.getAccentColor(ApplicationFactory.context!!))
}