簡體   English   中英

如何以編程方式更改這些視圖布局? 我在 xml 文件中有視圖,但后來想以編程方式更改它們的大小

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

我在 .xml 文件中有一個工作布局,這里沒問題。 在我的代碼中,有一個按鈕可以顯示或隱藏這兩個 imageView。 當我隱藏兩個圖像視圖時,我想將復選框布局約束設置為父級而不是匹配約束(“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>

我不想以編程方式創建 CheckBox 和兩個 ImageView,因為我之前已經在 .xml 文件中創建了它們。 我想要的是在 java 中以編程方式更改這些視圖。 提前致謝。

以編程方式在按鈕 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);
}

如果我理解你想要做什么,那么有一種簡單的編程方式:在onCreate()你聲明你的觀點

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

然后你在你的按鈕上設置一個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