简体   繁体   中英

How to change these views layouts programmatically? I have the views in xml file, but later want to change their size programmatically

I have a working layout in .xml file no problem here. In my code there is a button that show or hide these two imageViews. When I hide the two imageViews I want to set the checkbox layout constraints to mach parent instead of matching constraints("0dp"), and when I show the the two imageViews I want to set checkbox layout to match constraints.

<?xml version="1.0" encoding="utf-8"?>
<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/constraint_child"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<ImageView
    android:id="@+id/delete_child"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:layout_marginStart="16dp"
    android:src="@drawable/delete"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/edit_child"
    app:layout_constraintTop_toTopOf="parent" />

<CheckBox
    android:id="@+id/checkbox_child"
    android:layout_width="0dp"
    android:layout_height="42dp"
    android:text="CheckBox"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/edit_child"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/edit_child"
    android:layout_width="26dp"
    android:layout_height="26dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/delete_child"
    app:layout_constraintStart_toEndOf="@+id/checkbox_child"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/edit" />


</androidx.constraintlayout.widget.ConstraintLayout>

I don't want to create the CheckBox and the two ImageViews programmatically ,because I have created them before in .xml file. what I want is changing these views programmatically in java. Thanks in advance.

programaticlly on button onclicklistner When images are gone you can set

Checkbox checkbox = findViewById(R.id.checkbox_child);
    
if (myImageView.getVisibility() == View.VISIBLE) {
    // Its visible
 

LayoutParams lp = new LayoutParams(0, 42);
checkbox.setLayoutParams(lp);

} else {
    // Either gone or invisible

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 42);
checkbox.setLayoutParams(lp);
}

If i understood what you are trying to do, then there is a simple way of doing it programatically : in onCreate() you declare your views

ImageView deleteIv = findViewById(R.id.delete_child);
ImageView editIv = findViewById(R.id.edit_child);
CheckBox checkbox = findViewById(R.id.checkbox_child);

Then you set an onClickListener over your button and you have to put this code inside of onClick() method generated by the button's listener

yourButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                deleteIv.setVisibility(View.GONE);
                editIv.setVisibility(View.GONE);
                
                // Creating a new LayoutParams object to set to the checkBox
                
                LinearLayout.LayoutParams lparams = new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATH_PARENT);
                // Setting the new layout parameters to the checkbox as wanted
                checkbox.setLayoutParams(lparams);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM