添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
一身肌肉的茄子  ·  Troubles connection ...·  2 周前    · 
近视的仙人掌  ·  Python ...·  2 年前    · 
个性的莴苣  ·  python - SQLAlchemy ...·  2 年前    · 
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

I need to change margin of toolbar, which was made of ConstraintLayout, in different cases. I tried to do it in following way

ConstraintLayout.LayoutParams newLayoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
ConstraintLayout.MarginLayoutParams layoutParams = new ConstraintLayout.MarginLayoutParams(newLayoutParams);
layoutParams.setMargins(0, 0, 0, 0);
toolbar.setLayoutParams(newLayoutParams);

but in second case layoutParams.setMargins(16, 16, 16, 16); it did not change. So, can someone give other way or point to the mistake. Thanks for spending time to my problem.

I tried to use newLayoutParams.setMargins(54, 54, 54, 0); this puts margin to left and right, but I still need to put margin on top of it.

Finally with help of @Aksh I found my mistake and solve my problem. If someone find it useful, I will put my code bellow

     ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams) toolbar.getLayoutParams();
     newLayoutParams.topMargin = 0;
     newLayoutParams.leftMargin = 0;
     newLayoutParams.rightMargin = 0;
     toolbar.setLayoutParams(newLayoutParams);
                and if its not working its most probably because the layout is not connected with the parent constraintSet.connect(testButton.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
– Ultimo_m
                Apr 5, 2019 at 9:48
                In case if you are using Kotli ,             var newLayoutParams = ConstraintLayout.LayoutParams(yourLayout.layoutParams)             newLayoutParams.rightMargin = 16             yourLayout.layoutParams = newLayoutParams
– mask
                Aug 18, 2020 at 4:30
                This solution works, but you have to use pixels instead of dp for the values. This answer stackoverflow.com/a/56550964/3697107 below by 6rchid helped me.
– Andrew Lee
                Jul 27, 2021 at 8:14

good way to add margin to a view in constraintlayout

val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayoutId)
val marginTop = context.getDimension(10f)
constraintSet.setMargin(R.id.tv_user, ConstraintSet.TOP, marginTop)
constraintSet.applyTo(constraintLayoutId)
fun Context.getDimension(value: Float): Int {
    return TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, value, this.resources.displayMetrics).toInt()

I once modified a ConstraintLayout with a ConstraintSet to change the elements it got linked with. I also changed the margin within that process which is the last parameter of the connect method:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout);
constraintSet.connect(
    R.id.layout_id, 
    ConstraintSet.START, 
    R.id.layout_id2, 
    ConstraintSet.END, 
constraintSet.applyTo(layout);

Maybe this gives you another hint to find a solution?

Thanks I tired all the above solutions but margins were not set after applying the new constraints using the above code to the layout it worked – SagaRock101 Jul 27, 2021 at 10:25 I finally found something that works. They key takeaways I have now is: copy parent constraintset and apply to same parent, connect layouts. – Warpzit Aug 25, 2022 at 12:27

You might wanna try

ViewGroup.MarginLayoutParams params = yourConstraintLayout.getLayoutParams();
params.topMargin = x;
//others shows up in suggestion

I haven't tried it though I believe it should work as ConstraintLayout is a ViewGroup.

For layoutParams.setMargins(w, x, y, z) the reason it's not changing is because you're most likely treating the values as Dp's rather than Px's, for which the method accepts.

To get Px from the Dp, you want to use:

int pxValue = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());

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.