android - 按下imageview时更改所有背景的源图像

标签 android android-layout kotlin

当我单击ImageView时,我想更改所有图层的背景,例如,此ImageView的预览为黄色,但是当我单击它时,我想将背景更改为名称为fondoamarillo.jpg的图像,即图层名称是colores,我的kt是Colores

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/mainln"
    android:layout_height="match_parent"
    android:background="@drawable/fondoazul">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="175dp"
        android:layout_height="104dp"
        android:layout_marginStart="70dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="71dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/iccol" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="200dp"
        android:layout_height="52dp"
        android:layout_marginStart="104dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="104dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.333"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2"
        app:srcCompat="@drawable/coloress" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="28dp"
        android:onClick="onAtrasConfig"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/atras" />

    <ImageView
        android:id="@+id/imageView42"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="41dp"
        android:layout_marginTop="89dp"
        android:layout_marginBottom="214dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView44"
        app:layout_constraintTop_toBottomOf="@+id/imageView45"
        app:srcCompat="@drawable/verde" />

    <ImageView
        android:id="@+id/imageView45"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="41dp"
        android:layout_marginTop="55dp"
        app:layout_constraintStart_toEndOf="@+id/imageView41"
        app:layout_constraintTop_toBottomOf="@+id/imageView3"
        app:srcCompat="@drawable/aguamarina" />

    <ImageView
        android:id="@+id/imageView41"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="55dp"
        android:onClick="changeAllBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView3"
        app:srcCompat="@drawable/amarillo" />

    <ImageView
        android:id="@+id/imageView46"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="41dp"
        android:layout_marginTop="89dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="214dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView42"
        app:layout_constraintTop_toBottomOf="@+id/imageView43"
        app:srcCompat="@drawable/rosa" />

    <ImageView
        android:id="@+id/imageView44"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="11dp"
        android:layout_marginTop="89dp"
        android:layout_marginBottom="214dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView41"
        app:srcCompat="@drawable/azul" />

    <ImageView
        android:id="@+id/imageView43"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="41dp"
        android:layout_marginTop="55dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView45"
        app:layout_constraintTop_toBottomOf="@+id/imageView3"
        app:srcCompat="@drawable/rojo" />

</androidx.constraintlayout.widget.ConstraintLayout>
这是.kt:
class Colores : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.colores)
    }

    fun onAtrasConfig(view: View){
        try {
            val intent: Intent = Intent(this, Configuracion::class.java)
            startActivity(intent)
        }catch (e: Exception){
            e.printStackTrace()
        }
    }

    fun changeAllBackground(mainLayout:View) {
        val mainln: ConstraintLayout = findViewById(R.id.mainln)
        mainln.background = getDrawable(R.drawable.fondoamarillo)
    }
}
This is my xml and I would like to click the imageView or Button "it doesn't matter which one if it works" to change the image of the layout background
This should be the new background when I click the new imageView or Button

最佳答案

好的,那么您可以使用我刚刚创建的此功能。它始终需要您的 View 持有者的布局。例如,如果您想捕获所有图层,则可以发送自己的主布局,无论哪种主布局都可以,并且可以将背景更改为您告诉我的图像,这并不重要。

Java版本:

    private void changeAllBackground(ViewGroup mainLayout){
        for(int i=0; i<mainLayout.getChildCount();i++){
            View v = mainLayout.getChildAt(i);
            //this is where you put what you want to do with the layer
            v.setBackgroundResource(R.drawable.fondoamarillo);
            if(v instanceof ViewGroup)
                changeAllBackground(((ViewGroup)v));
        }
    }

Kotlin版本:
private fun changeAllBackground(mainLayout:ViewGroup) {
  for (i in 0 until mainLayout.getChildCount())
  {
    val v = mainLayout.getChildAt(i)
    //this is where you put what you want to do with the layer
    v.setBackgroundResource(R.drawable.fondoamarillo)
    if (v is ViewGroup){
        changeAllBackground((v as ViewGroup))
    }
  }
}

关于android - 按下imageview时更改所有背景的源图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61336372/

相关文章:

android - 为什么这里在StartActivityForResult之后是resultCode = -1?

android - signingConfigs 标签在升级到 v22 后在 build.gradle 中给我一个 Lint 错误

android - Android 的 invalidate() 和 postInvalidate() 方法有什么区别?

android - 如何从 fragment 中获取 Activity 的名称?

当系统范围的字体放大时,Android TextView 不包装内容

Android复合控件不会显示

Android recyclerview 在 kotlin 中具有不同的 View 类型

kotlin - 创建 jvm 和 Kotlin 多平台的 Github 库组合

android - 分页库 3.0 : How to pass total count of items to the list header?

Android 评分栏超出对话框范围