它没有边距而膨胀视图

时间:2011-10-10 14:33:19

标签: android margin inflate android-inflate layout-inflater

我有这段代码

View item = View.inflate(context, R.layout.item_layout, null);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT);
    layout.addView(item, params);

我的item_layout :(注意部分android:layout_marginTop =“2dip”)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_width="fill_parent">

    <ImageView android:src="@drawable/pic_unknown" android:id="@+id/image1"
        android:layout_height="50dip" android:layout_width="50dip"
        android:padding="5dip"></ImageView>
</RelativeLayout>

然后在我的布局中,我看到项目列表已经膨胀,但它们之间没有边距。我尝试使用margintop = 10dip仍然没有发生我的观点是我在布局中放置的值不是在计算中带有或没有边距顶部布局是相同的。

如何在项目之间添加一些空白区域? 如何在物品之间充气? 是否有可能膨胀像空隙或某些空间? 或者我必须使用解决方法,比如用2dip高度或其他东西来膨胀一些空布局 感谢

3 个答案:

答案 0 :(得分:20)

inflate方法的最后一个参数是添加膨胀视图的参数。在您的情况下,它是null。试试这个:

 View item = View.inflate(context, R.layout.item_layout, layout);

答案 1 :(得分:3)

如果您的边距适用于外部,请尝试填充RelativeLayout。

答案 2 :(得分:0)

您可以为您夸大的布局添加边距,如下所示:

final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                                ViewGroup.LayoutParams.WRAP_CONTENT);
         params.topMargin = 10;
相关问题