How to set margins for TextView programmatically?

后端 未结 4 1526
清酒与你
清酒与你 2020-12-14 05:46
TextView tv1 = new TextView(this);      
tv1.setPadding(5, 0, 5, 0);
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
tv1         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 06:02

    All these answers are great, but I was using ConstraintLayout, so here is code for that:

    ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(10, 10, 10, 10);
    textview.setLayoutParams(params); // note that textview would be your instanced TextView object
    

提交回复
热议问题