在 Constraint Layout 中添加视图并以编程方式将它们设置为全屏
Add views inside Constraint Layout and set them full screen PROGRAMMATICALLY
我花了几个小时在 ConstraintLayout 中添加一个图像视图并将其设置为全屏大小。
这是我在约束布局中添加图像的实际代码:
final ConstraintLayout constraitLayout = (ConstraintLayout)activity.findViewById(layout_xml);
constraitLayout.addView(mImage,0);
ConstraintSet set1 = new ConstraintSet();
set1.clone(constraitLayout);
set1.connect(mImage.getId(), ConstraintSet.TOP, constraitLayout.getId(), ConstraintSet.TOP, 0);
set1.connect(mImage.getId(), ConstraintSet.LEFT, constraitLayout.getId(), ConstraintSet.LEFT, 0);
set1.connect(mImage.getId(), ConstraintSet.RIGHT, constraitLayout.getId(), ConstraintSet.RIGHT, 0);
set1.connect(mImage.getId(), ConstraintSet.BOTTOM, constraitLayout.getId(), ConstraintSet.BOTTOM, 0);
set1.applyTo(constraitLayout);
感谢帮助!
问题是您没有为图像视图设置宽度和高度,因此默认情况下两者都采用 wrap_content。所以将它们都设置为 0dp 将解决问题。添加这些行以将宽度和高度设置为 0dp。
mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));
还有一件事,您可以使用 ConstraintSet.PARENT_ID 而不是将父 ID 作为 constraintLayout.getId()。所以最终代码看起来像
mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));
constraitLayout.addView(mImage,0);
ConstraintSet set1 = new ConstraintSet();
set1.clone(constraitLayout);
set1.connect(mImage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set1.connect(mImage.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set1.connect(mImage.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set1.connect(mImage.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set1.applyTo(constraitLayout);
希望这对您有所帮助
我花了几个小时在 ConstraintLayout 中添加一个图像视图并将其设置为全屏大小。
这是我在约束布局中添加图像的实际代码:
final ConstraintLayout constraitLayout = (ConstraintLayout)activity.findViewById(layout_xml);
constraitLayout.addView(mImage,0);
ConstraintSet set1 = new ConstraintSet();
set1.clone(constraitLayout);
set1.connect(mImage.getId(), ConstraintSet.TOP, constraitLayout.getId(), ConstraintSet.TOP, 0);
set1.connect(mImage.getId(), ConstraintSet.LEFT, constraitLayout.getId(), ConstraintSet.LEFT, 0);
set1.connect(mImage.getId(), ConstraintSet.RIGHT, constraitLayout.getId(), ConstraintSet.RIGHT, 0);
set1.connect(mImage.getId(), ConstraintSet.BOTTOM, constraitLayout.getId(), ConstraintSet.BOTTOM, 0);
set1.applyTo(constraitLayout);
感谢帮助!
问题是您没有为图像视图设置宽度和高度,因此默认情况下两者都采用 wrap_content。所以将它们都设置为 0dp 将解决问题。添加这些行以将宽度和高度设置为 0dp。
mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));
还有一件事,您可以使用 ConstraintSet.PARENT_ID 而不是将父 ID 作为 constraintLayout.getId()。所以最终代码看起来像
mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));
constraitLayout.addView(mImage,0);
ConstraintSet set1 = new ConstraintSet();
set1.clone(constraitLayout);
set1.connect(mImage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set1.connect(mImage.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set1.connect(mImage.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set1.connect(mImage.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set1.applyTo(constraitLayout);
希望这对您有所帮助