如何在LinearLayout子项之间腾出空间? [英] How to make space between LinearLayout children?
问题描述
我正在以编程方式将自定义视图添加到垂直的LinearLayout中,并且我希望这些视图之间有一定的空间.我尝试将setPadding(0,1,0,1)添加到我的CustomView构造函数中,但这似乎没有任何效果.有什么建议吗?
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) to my CustomView constructor, but this doesn't seem to have any effect. Any advice?
*有人指出,我应该使用页边距.由于我是动态添加视图,因此需要从代码中设置边距(不在xml中).我相信下面是执行此操作的方法,但是它不起作用.
*It was pointed out that I should use margins. Since I am dynamically adding views, I need to set the margins from code (not in xml). I believe the way to do this is below, but it isn't working.
public class MyView extends View
{
public MyView (Context context)
{
super(context);
MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 10, 0, 10);
setLayoutParams(params);
*编辑.我还在将视图添加到线性布局时尝试使用MarginLayoutParams作为参数(如下所示).这也行不通:
*Edit. I also tried using MarginLayoutParams as a parameter while adding the views to the Linear layout (as below). This also did not work:
MarginLayoutParams params = new MarginLayoutParams(linearLayout.getLayoutParams());
linearLayout.setMargins(0, 10, 0, 10);
linearLayout.addView(view, params);
推荐答案
您应该在子级上android:layout_margin<Side>
.填充是内部的.
You should android:layout_margin<Side>
on the children. Padding is internal.
这篇关于如何在LinearLayout子项之间腾出空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!