当我单击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)
}
}
最佳答案
好的,那么您可以使用我刚刚创建的此功能。它始终需要您的 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/