17

我有这个代码

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 仍然没有发生我的观点是我在布局中输入的值在计算中没有考虑到有或没有边距顶部布局是相同的。

如何在项目之间添加一些空白空间?如何充气项目之间的空白空间?是否可以膨胀诸如 gap 或 some space 之类的东西?或者我必须使用解决方法,比如用 2dip 高度或其他东西膨胀一些空布局谢谢

4

3 回答 3

23

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

 View item = View.inflate(context, R.layout.item_layout, layout);
于 2011-10-10T14:37:42.850 回答
3

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

于 2011-10-10T14:40:07.143 回答
0

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

final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                                ViewGroup.LayoutParams.WRAP_CONTENT);
         params.topMargin = 10;
于 2015-10-07T10:07:19.023 回答