Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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
Android 使用kotliin更改背景色调_Android_Kotlin_Background Color - Fatal编程技术网

Android 使用kotliin更改背景色调

Android 使用kotliin更改背景色调,android,kotlin,background-color,Android,Kotlin,Background Color,我用Kotlin创建了一个Android应用程序,我有以下问题: 在我的一个UI中,我创建了一个约束布局,该布局具有以下内容: <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="35dp" android:layout_height="35dp" android:layout_marginTop="5dp" android:layout_marginEnd="5d

我用Kotlin创建了一个Android应用程序,我有以下问题: 在我的一个UI中,我创建了一个约束布局,该布局具有以下内容:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="5dp"
    android:id="@+id/cart"
    android:background="@drawable/circle_vs_shape"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:tint="@color/whiteFour"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/add_cart" />
</androidx.constraintlayout.widget.ConstraintLayout>

如何更正代码以更改背景色调

就个人而言,有几种方法可以解决这个问题:

    ConstraintLayout someLayout=(ConstraintLayout)view.findViewById(R.id.someLayout);
    someLayout.getBackground().setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP);
如果它是一个规则资源,背景是一个图像而不是向量,您可以在“颜色”文件夹中创建一个颜色选择器

您还可以使用两个具有不同图像和不同色调的图像,而不是容器和imageview

编辑

将其放在“颜色”文件夹中

电子邮件\u phone\u color\u selector.xml

并使用它

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cart"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginTop="5dp"
    android:background="@color/email_phone_color_selector"
    app:tint="@color/colorAccent">

圆和形状是矢量资源吗?不,它只是可绘制的资源文件中的一个黑色圆。我编辑了我的答案
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/sms_phone_disabled"/>
    <item android:state_pressed="true" android:color="@color/sms_phone_disabled"/>
    <item android:color="@color/sms_phone_enabled"/>
</selector>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cart"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginTop="5dp"
    android:background="@color/email_phone_color_selector"
    app:tint="@color/colorAccent">