如何在ConstraintLayout中通过代码复制隐藏控件的布局属性
Absolutely! You can replicate the XML changes you made entirely in code using Android's ConstraintSet API, which lets you modify ConstraintLayout constraints dynamically. Here's exactly how to do it:
Step-by-Step Implementation
First, add an ID to your ConstraintLayout (you didn’t include one in your original XML—add android:id="@+id/main_constraint_layout" to the ConstraintLayout tag), then get references to your views and adjust constraints:
Kotlin Code
// Get references to your views val constraintLayout = findViewById<ConstraintLayout>(R.id.main_constraint_layout) val someTitle = findViewById<TextView>(R.id.some_title) val textView1 = findViewById<TextView>(R.id.textView1) val textView2 = findViewById<TextView>(R.id.textView2) // Create a ConstraintSet and clone the current layout constraints val constraintSet = ConstraintSet() constraintSet.clone(constraintLayout) // 1. Hide textView1 constraintSet.setVisibility(textView1.id, View.GONE) textView1.visibility = View.GONE // Sync the view's visibility directly // 2. Copy textView1's positioning constraints to textView2 // Align start to parent start with matching margin constraintSet.connect( textView2.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 16 ) // Align top to someTitle's bottom with matching margin constraintSet.connect( textView2.id, ConstraintSet.TOP, someTitle.id, ConstraintSet.BOTTOM, 8 ) // Align bottom to parent bottom with matching margin constraintSet.connect( textView2.id, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 20 ) // Clear textView2's old end constraint (previously tied to parent end) constraintSet.clear(textView2.id, ConstraintSet.END) // 3. Update someTitle's bottom constraint to point to textView2 instead of textView1 constraintSet.connect( someTitle.id, ConstraintSet.BOTTOM, textView2.id, ConstraintSet.TOP, 8 ) // 4. Apply textView1's additional layout properties to textView2 constraintSet.setHorizontalBias(textView2.id, 0f) // Match horizontal bias constraintSet.setConstrainedWidth(textView2.id, true) // Enable constrained width constraintSet.setWidth(textView2.id, ConstraintSet.WRAP_CONTENT) // Change width from 0dp to wrap_content // Update view-specific attributes (not managed by ConstraintSet) textView2.apply { setPadding(0, 0, 0, 0) // Remove original top padding maxLines = 1 layoutParams = (layoutParams as ConstraintLayout.LayoutParams).apply { marginEnd = 8 // Match textView1's end margin maxWidth = resources.getDimensionPixelSize(R.dimen.your_88dp) // Keep maxWidth if needed } } // 5. Apply the updated constraints to the layout constraintSet.applyTo(constraintLayout)
Java Code
// Get references to your views ConstraintLayout constraintLayout = findViewById(R.id.main_constraint_layout); TextView someTitle = findViewById(R.id.some_title); TextView textView1 = findViewById(R.id.textView1); TextView textView2 = findViewById(R.id.textView2); // Create a ConstraintSet and clone current constraints ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); // 1. Hide textView1 constraintSet.setVisibility(textView1.getId(), View.GONE); textView1.setVisibility(View.GONE); // 2. Copy textView1's positioning constraints to textView2 constraintSet.connect( textView2.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 16 ); constraintSet.connect( textView2.getId(), ConstraintSet.TOP, someTitle.getId(), ConstraintSet.BOTTOM, 8 ); constraintSet.connect( textView2.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 20 ); constraintSet.clear(textView2.getId(), ConstraintSet.END); // 3. Update someTitle's bottom constraint constraintSet.connect( someTitle.getId(), ConstraintSet.BOTTOM, textView2.getId(), ConstraintSet.TOP, 8 ); // 4. Apply additional layout properties constraintSet.setHorizontalBias(textView2.getId(), 0f); constraintSet.setConstrainedWidth(textView2.getId(), true); constraintSet.setWidth(textView2.getId(), ConstraintSet.WRAP_CONTENT); // Update view-specific attributes textView2.setPadding(0, 0, 0, 0); textView2.setMaxLines(1); ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) textView2.getLayoutParams(); params.setMarginEnd(8); params.maxWidth = getResources().getDimensionPixelSize(R.dimen.your_88dp); textView2.setLayoutParams(params); // 5. Apply constraints to the layout constraintSet.applyTo(constraintLayout);
Key Notes
- Make sure to replicate all margins, biases, and layout flags (like
constrainedWidth) to match your modified XML exactly. - Attributes like
maxLinesandpaddingare view-specific, so you’ll need to update them directly on the TextView instead of viaConstraintSet.
内容的提问来源于stack exchange,提问作者K.Os
相关产品推荐
相关产品推荐

