简体   繁体   English

如何以编程方式更改这些视图布局? 我在 xml 文件中有视图,但后来想以编程方式更改它们的大小

[英]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.我在 .xml 文件中有一个工作布局,这里没问题。 In my code there is a button that show or hide these two imageViews.在我的代码中,有一个按钮可以显示或隐藏这两个 imageView。 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.当我隐藏两个图像视图时,我想将复选框布局约束设置为父级而不是匹配约束(“0dp”),当我显示两个图像视图时,我想设置复选框布局以匹配约束。

<?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.我不想以编程方式创建 CheckBox 和两个 ImageView,因为我之前已经在 .xml 文件中创建了它们。 what I want is changing these views programmatically in java.我想要的是在 java 中以编程方式更改这些视图。 Thanks in advance.提前致谢。

programaticlly on button onclicklistner When images are gone you can set以编程方式在按钮 onclicklistner 当图像消失时,您可以设置

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如果我理解你想要做什么,那么有一种简单的编程方式:在onCreate()你声明你的观点

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然后你在你的按钮上设置一个onClickListener并且你必须把这个代码放在按钮的监听器生成的onClick()方法中

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);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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