How to make space between LinearLayout children?

后端 未结 13 558
一整个雨季
一整个雨季 2020-11-30 23:46

I am programatically adding custom views to a vertical LinearLayout, and I would like there to be some space between the views. I have tried adding: setPadding(0, 1, 0, 1)

13条回答
  •  遥遥无期
    2020-12-01 00:28

    You can get the LayoutParams of parent LinearLayout and apply to the individual views this way:

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(8,8,8,8);
    
    • Take care that setMargins() take pixels as int data type.So, convert to dp before adding values
    • Above code will set height and width to wrap_content. you can customise it.

提交回复
热议问题