添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
想发财的洋葱  ·  pcre2_match - man ...·  2 月前    · 
完美的莴苣  ·  标准来源·  3 月前    · 
会开车的钱包  ·  Spring for Apache ...·  4 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

For constraint layout v1.1.x in Android we can set height and width as percentage. Similarly, need to set view width and height as percent in Android programmatically: for example, this code is written in xml for some constraint layout:

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

what will be its java code for doing it runtime?

This class allows you to define programmatically a set of constraints to be used with ConstraintLayout. It lets you create and save constraints, and apply them to an existing ConstraintLayout. ConstraintsSet can be created in various ways:

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); 

You can call those height width percentage methods on this set

  • constrainPercentHeight(int viewId, float percent)
  • constrainPercentWidth(int viewId, float percent)
  • And apply those constraints to your Constraint Layout like this

    set.applyTo(mConstraintLayout); 
                    @adityKamble Thanks for solution, could you please let me know why question has been downvoted?
    – ghufranne
                    Jul 20, 2018 at 4:44
                    @ghufranne Not sure. Maybe some reviewers found it less in research. You can find more info here meta.stackoverflow.com/questions/252677/…
    – adityakamble49
                    Jul 20, 2018 at 5:09
                    There may be something wrong. if U set many constraints in xml, you'd better set.clone(mConstraintLayout) before set.constrainPercentWidth.
    – paperhs
                    Mar 26, 2021 at 7:50
    

    Not sure if this is better or worse, but there is another way to do this than the proposed answer:

    Kotlin:

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

    Java:

    (myView.layoutParams (ConstraintLayout.LayoutParams))
        .matchConstraintPercentWidth = value
    myView.requestLayout()
                    Thanks a lot. It constantly works well. And though it's not as beautiful as the answer before it works at least.
    – alexanderktx
                    Sep 2, 2020 at 13:17
                    Based on this, I used "View.updateLayoutParams(block: T.() -> Unit)" and it worked without the need of requestLayout(). Example: myView.updateLayoutParams<ConstraintLayout.LayoutParams> {             matchConstraintPercentWidth = value         }
    – Leandro Ocampo
                    Feb 17, 2021 at 10:54
    

    I found the answers above helpful, but still a little confusing. Here's what ultimately worked for me. There are 2 views involved in this example, a parent Constraint View and a child of the Constraint View.

    // 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);
    

    Note that for some reason, a percentage constraint of 1.0F doesn't work, although 0.99F works just fine.

    @MahdiJavaheri I can see how that would happen in adityakamble49's answer, since he applies a completely new contraintset to the existing view. However, I dont know whether the same goes for my answer, because I do not create or set a new LayoutParams object. I get the currently set LayoutParams, set the desired value and call for an update on the layout. Which should do the same as this answer, just without creating an unnecessary clone and replacing the old params with it. Not sure how these things work under the hood though, so Id be thankful for any futher explanation in this regard. – Benjamin Basmaci Jul 20, 2022 at 16:52

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.