它没有边距而膨胀视图

Luk*_*kap 17 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);
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

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

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

Fra*_*nke 21

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

 View item = View.inflate(context, R.layout.item_layout, layout);
Run Code Online (Sandbox Code Playgroud)

  • 我遇到的问题是我正在膨胀一个`View`来将它作为标题附加到`ListView`; 如果我在inflater中使用`ListView`它会引发异常.在这种情况下,我如何获得边距? (11认同)