Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 491
Lukap Avatar asked Oct 10 '11 14:10

Lukap


People also ask

What does it mean to inflate a view?

"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup.

What is layout inflate?

The LayoutInflater class is used to instantiate the contents of layout XML files into their corresponding View objects. In other words, it takes an XML file as input and builds the View objects from it.

What is inflate method?

As the title says, this post deals with LayoutInflater in Android and its most common usage with the inflate method. This method is used to create a view object from an XML file (where the UI designs are created). Then, you can have this view show up wherever you want (of course, in your app, not your freezer 😋).

How do I set custom view margins?

You can specify your own custom margin settings. Click Margins, click Custom Margins, and then in the Top, Bottom, Left, and Right boxes, enter new values for the margins.


2 Answers

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);
like image 104
Franziskus Karsunke Avatar answered Sep 30 '22 12:09

Franziskus Karsunke


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

like image 3
coder_For_Life22 Avatar answered Sep 30 '22 12:09

coder_For_Life22