开发者

It inflate the view without the margin

I have this code

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);

my item_layout: (note the part 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>

and then in my layout I see the list of items inflated but with no margin in-between them. I tried with margintop=10dip still nothings happen my point is that the value I put in the layout it is not taken in the calculation 开发者_开发百科with or without the margin top the layout is the same.

How can I add some empty space between the items ? How can I inflate a empty space between the items ? Is it possible to inflate something like gap or some space ? or I must use workaround like inflating some empty layout with 2dip height or something Thanks


The last parameter of the inflate method is the parameter to which you add the inflated view. In your case it is null. Try this instead:

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


Try Padding the RelativeLayout instead if your margins apply to the outside.


You can add margin to layout which you inflated like below:

final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                                ViewGroup.LayoutParams.WRAP_CONTENT);
         params.topMargin = 10;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜