如何使用 android 中的 java 更改约束布局背景图像
How to change constraint layout background image using java in android
我正在尝试更改约束布局背景图像。单击两个单选按钮中的任何一个,背景图像都应该改变。我附上了下面的代码。我在 radioGroup 上设置了一个 OnCheckedChangeListener。
Java 文件
package com.mitwpu.practicallab_6_2_2020;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Change_Background_Image_Activity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButtonTom;
RadioButton radioButtonJerry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change__background__image_);
radioGroup=(RadioGroup)findViewById(R.id.radioGroupId);
radioButtonTom=(RadioButton)findViewById(R.id.radioButtonTom);
radioButtonJerry=(RadioButton)findViewById(R.id.radioButtonJerry);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButtonTom : {
//R.drawable.tom ->image1
break;
}
case R.id.radioButtonJerry : {
//R.drawable.jerry->image2
break;
}
}
}
});
}
}
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/backgroudId"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Change_Background_Image_Activity">
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="274dp"
android:layout_height="448dp"
android:layout_marginStart="79dp"
android:layout_marginTop="116dp"
android:layout_marginEnd="58dp"
android:layout_marginBottom="167dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButtonTom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tom" />
<RadioButton
android:id="@+id/radioButtonJerry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jerry" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
单击单选按钮后如何更改背景图像
constraintId.setBackground(ContextCompat.getDrawable(this, R.drawable.some_drawable))
是的,上面的答案肯定有效。您可以通过在 switch case 中调用 findViewById(R.id.backgroudId).setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable_here))
来在 constraintLayout 上设置背景图像。但是,我也可以建议您为背景图像创建一个单独的 ImageView,因为 ImageViews 在 scaleType 等方面具有更大的灵活性。我还在您的代码下方的代码中包含了较少的边距等。如果您没有明确声明 ConstraintLayout 将为您处理很多边距问题,因此通过将 radioGroup 设置为在 top/bottom/start/end 上约束到父级,它将自动居中在屏幕中间。所以考虑尝试这样的事情:
<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:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/backgroundId"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButtonTom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tom" />
<RadioButton
android:id="@+id/radioButtonJerry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jerry" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
这将使您的图像更加灵活。我还将包含用于图像切换的代码,尽管它是在 Kotlin 中:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listener = RadioGroup.OnCheckedChangeListener { _, checkedId ->
when (checkedId) {
radioButtonTom.id -> {
backgroundId.background = ContextCompat.getDrawable(applicationContext, R.drawable.ic_android_black_24dp)
}
radioButtonJerry.id -> {
backgroundId.background = ContextCompat.getDrawable(applicationContext, R.drawable.ic_launcher_background)
}
}
}
radioGroupId.setOnCheckedChangeListener(listener)
}
}
我正在尝试更改约束布局背景图像。单击两个单选按钮中的任何一个,背景图像都应该改变。我附上了下面的代码。我在 radioGroup 上设置了一个 OnCheckedChangeListener。
Java 文件
package com.mitwpu.practicallab_6_2_2020;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Change_Background_Image_Activity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButtonTom;
RadioButton radioButtonJerry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change__background__image_);
radioGroup=(RadioGroup)findViewById(R.id.radioGroupId);
radioButtonTom=(RadioButton)findViewById(R.id.radioButtonTom);
radioButtonJerry=(RadioButton)findViewById(R.id.radioButtonJerry);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButtonTom : {
//R.drawable.tom ->image1
break;
}
case R.id.radioButtonJerry : {
//R.drawable.jerry->image2
break;
}
}
}
});
}
}
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/backgroudId"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Change_Background_Image_Activity">
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="274dp"
android:layout_height="448dp"
android:layout_marginStart="79dp"
android:layout_marginTop="116dp"
android:layout_marginEnd="58dp"
android:layout_marginBottom="167dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButtonTom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tom" />
<RadioButton
android:id="@+id/radioButtonJerry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jerry" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
单击单选按钮后如何更改背景图像
constraintId.setBackground(ContextCompat.getDrawable(this, R.drawable.some_drawable))
是的,上面的答案肯定有效。您可以通过在 switch case 中调用 findViewById(R.id.backgroudId).setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable_here))
来在 constraintLayout 上设置背景图像。但是,我也可以建议您为背景图像创建一个单独的 ImageView,因为 ImageViews 在 scaleType 等方面具有更大的灵活性。我还在您的代码下方的代码中包含了较少的边距等。如果您没有明确声明 ConstraintLayout 将为您处理很多边距问题,因此通过将 radioGroup 设置为在 top/bottom/start/end 上约束到父级,它将自动居中在屏幕中间。所以考虑尝试这样的事情:
<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:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/backgroundId"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButtonTom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tom" />
<RadioButton
android:id="@+id/radioButtonJerry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jerry" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
这将使您的图像更加灵活。我还将包含用于图像切换的代码,尽管它是在 Kotlin 中:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listener = RadioGroup.OnCheckedChangeListener { _, checkedId ->
when (checkedId) {
radioButtonTom.id -> {
backgroundId.background = ContextCompat.getDrawable(applicationContext, R.drawable.ic_android_black_24dp)
}
radioButtonJerry.id -> {
backgroundId.background = ContextCompat.getDrawable(applicationContext, R.drawable.ic_launcher_background)
}
}
}
radioGroupId.setOnCheckedChangeListener(listener)
}
}