约束布局不能将宽度或高度设置为0

时间:2019-09-17 18:04:16

标签: android android-constraintlayout layoutparams

由于ConstraintLayout的事实,我如何通过LayoutParamsConstraintLayot.LayoutParams.MATCH_CONSTRAINT = 0中以编程方式将实际宽度或高度设置为 0

我想在我的代码中使用如下代码:

view.getLayoutParams().width = 0;
view.requestLayout();

4 个答案:

答案 0 :(得分:0)

如果要将宽度更改为MATCH_CONSTRAINT,则需要设置具有水平约束的布局

layout_constraintStart_toStartOflayout_constraintStart_toEndOf

layout_constraintEnd_toEndOflayout_constraintEnd_toStartOf

答案 1 :(得分:0)

如果用“高度”表示ImageView的从上到下的绝对尺寸,则可以执行以下操作将ImageView的高度加倍。您可以将高度设置为所需的值。

ImageView iv = (ImageView) findViewById(R.id.imageView); 
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) 
iv.getLayoutParams(); lp.height = 0 ; iv.setLayoutParams(lp);

请参见ConstraintLayout.LayoutParams。

答案 2 :(得分:0)

这是示例代码,对我有用

val params = ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_CONSTRAINT,
                    ConstraintLayout.LayoutParams.WRAP_CONTENT
                )
                val space16 = binding.root.resources.getDimensionPixelOffset(R.dimen.space_16)
                params.setMargins(space16, 0, 0, 0)

                /*app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/imgIcon1"
                app:layout_constraintTop_toTopOf="parent"*/

                params.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
                params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
                params.startToEnd = binding.imgIcon1.id
                params.topToTop = ConstraintLayout.LayoutParams.PARENT_ID


                binding.containerBottom.layoutParams = params

答案 3 :(得分:0)

我找到了解决方法。 所以根据这个

<attr name="layout_height" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>

-1 用于 ma​​tch_parent

-2 用于 wrap_content

所以我们可以使用 -3 来设置 0 或小于 0 的宽度或高度值。

例如

android:layout_width="-3dp"