在Android中,如何的LinearLayout孩子之间腾出空间? [英] In Android, 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?
*有人指出,我应该使用的利润率。因为我动态添加的看法,我需要设置从code中的利润(非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);
感谢你。
推荐答案
您应该安卓layout_margin<侧>
的孩子。填充是内部的。
You should android:layout_margin<Side>
on the children. Padding is internal.
这篇关于在Android中,如何的LinearLayout孩子之间腾出空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!