17

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

Lukap
  • 31,523
  • 64
  • 157
  • 244

3 Answers3

23

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);
Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
3

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

coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
0

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;
savepopulation
  • 11,736
  • 4
  • 55
  • 80