【问题标题】:Constraint Layout can't set width or height to 0约束布局无法将宽度或高度设置为 0
【发布时间】:2020-01-18 15:26:34
【问题描述】:

我如何通过LayoutParams 以编程方式在ConstraintLayout 中将实际宽度或高度设置为0,因为ConstraintLayot.LayoutParams.MATCH_CONSTRAINT = 0 的事实是?

我想在我的代码中使用这样的东西:

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

【问题讨论】:

    标签: android android-constraintlayout layoutparams


    【解决方案1】:

    我找到了解决方法。 所以按照这个

    <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"
    

    【讨论】:

      【解决方案2】:

      如果“高度”是指 ImageView 从上到下的绝对尺寸,您可以执行以下操作将 ImageView 的高度加倍。您可以将高度设置为您想要的值。

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

      请参阅 ConstraintLayout.LayoutParams。

      【讨论】:

      • 我想真的是0
      【解决方案3】:

      这是尝试的示例代码,它适用于我

      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
      

      【讨论】:

        【解决方案4】:

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

        layout_constraintStart_toStartOflayout_constraintStart_toEndOf

        layout_constraintEnd_toEndOflayout_constraintEnd_toStartOf

        【讨论】:

        • 我在哪里说过我想将它设置为 MATCH_CONSTRAINT?我想设置为 0...
        • 你需要同时设置 layout_constraintStart_toStartOf 和 layout_constraintEnd_toEndOf 然后 MATCH_CONSTRAINT 才能工作
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 2021-08-12
        • 1970-01-01
        • 2021-10-30
        • 2021-10-28
        相关资源
        最近更新 更多