【问题标题】:set percent width constraintlayout Android programmatically以编程方式设置百分比宽度约束布局Android
【发布时间】:2018-07-19 10:57:22
【问题描述】:

对于 Android 中的约束布局 v1.1.x,我们可以将高度和宽度设置为百分比。 同样,需要以编程方式在 Android 中将视图宽度和高度设置为百分比: 例如,这段代码是用 xml 编写的,用于一些约束布局:

<!-- the widget will take 40% of the available space -->
    app:layout_constraintWidth_default="percent"
    app:layout_constraintWidth_percent="0.4"

运行时的 java 代码是什么?

【问题讨论】:

    标签: java android android-constraintlayout


    【解决方案1】:

    你需要使用 ConstraintSet - Reference

    该类允许您以编程方式定义一组与ConstraintLayout一起使用的约束。它允许您创建和保存约束,并将它们应用到现有的 ConstraintLayout。 ConstraintsSet 可以通过多种方式创建:

    mConstraintLayout = (ConstraintLayout) findViewById(R.id.myconstraint_layout)
    
    ConstraintSet set = new ConstraintSet();
    
    // Add constrains - Here R.id.myconstraint_layout is the Id of your constraint layout
    set.constrainPercentHeight(R.id.myconstraint_layout, 0.4);
    set.constrainPercentWidth(R.id.myconstraint_layout, 0.4);
    
    // Apply the changes - mConstraintLayout is reference to the desired view
    set.applyTo(mConstraintLayout); 
    

    你可以在这个集合上调用那些高宽百分比方法

    并像这样将这些约束应用到您的约束布局

    set.applyTo(mConstraintLayout); 
    

    【讨论】:

    • @adityKamble 感谢您的解决方案,能否请您告诉我为什么问题被否决?
    • @ghufranne 不确定。也许一些评论家在研究中发现它较少。你可以在这里找到更多信息meta.stackoverflow.com/questions/252677/…
    • 我认为它被否决的原因是因为它至少现在不起作用
    • 可能有问题。如果你在xml中设置了很多约束,你最好在set.constrainPercentWidth之前set.clone(mConstraintLayout)。
    【解决方案2】:

    不确定这是好是坏,但除了建议的答案之外,还有另一种方法:

    科特林:

    (myView.layoutParams as ConstraintLayout.LayoutParams)
        .matchConstraintPercentWidth = value
    myView.requestLayout()
    

    Java:

    (myView.layoutParams (ConstraintLayout.LayoutParams))
        .matchConstraintPercentWidth = value
    myView.requestLayout()
    

    【讨论】:

    • 不错。为我工作!
    • 非常感谢。它一直运行良好。尽管它至少在它起作用之前不如答案那么漂亮。
    • 基于此,我使用了“View.updateLayoutParams(block: T.() -> Unit)”,它不需要 requestLayout() 就可以工作。示例:myView.updateLayoutParams { matchConstraintPercentWidth = value }
    【解决方案3】:

    我发现上面的答案很有帮助,但仍然有点令人困惑。这就是最终对我有用的方法。此示例中涉及 2 个视图,一个父约束视图和一个约束视图的子视图。

    // Get the constraint layout of the parent constraint view.
    ConstraintLayout mConstraintLayout = findViewById(R.id.parentView);
    
    // Define a constraint set that will be used to modify the constraint layout parameters of the child.
    ConstraintSet mConstraintSet = new ConstraintSet();
    
    // Start with a copy the original constraints.
    mConstraintSet.clone(mConstraintLayout);
    
    // Define new constraints for the child (or multiple children as the case may be).
    mConstraintSet.constrainPercentWidth(R.id.childView, 0.5F);
    mConstraintSet.constrainPercentHeight(R.id.childView, 0.7F);
    
    // Apply the constraints for the child view to the parent layout.
    mConstraintSet.applyTo(mConstraintLayout);
    

    请注意,由于某种原因,1.0F 的百分比约束不起作用,尽管 0.99F 可以正常工作。

    【讨论】:

    • 这是必须接受的答案,不克隆,现有的约束将丢失
    猜你喜欢
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 2021-11-22
    相关资源
    最近更新 更多