如何在android中使用java更改约束布局背景图像

问题描述 投票:0回答:2

我正在尝试更改约束布局背景图像。单击两个单选按钮之一时,背景图像应更改。我已经附上了下面的代码。我在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>

单击单选按钮后如何更改背景图像

android background-image android-constraintlayout android-radiobutton
2个回答
1
投票
constraintId.setBackground(ContextCompat.getDrawable(this, R.drawable.some_drawable))

0
投票

是,以上答案肯定会有效。您可以通过在切换条件下调用findViewById(R.id.backgroudId).setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable_here))来在constraintLayout上设置背景图像。但是,我也建议您为背景图像创建一个单独的ImageView,因为ImageViews在scaleType等方面具有更大的灵活性。我还在代码下面的代码中使用了较少的边距,等等。如果您未明确声明ConstraintLayout,它将为您处理很多页边距内容,因此,通过将radioGroup限制在top / bottom / start / end设置为parent,它将自动在屏幕中间居中。因此,考虑尝试这样的事情:

<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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.