-2

I created this layout

<LinearLayout
    android:id="@+id/list"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="20dp"
    android:layout_weight=".7" >
    <RelativeLayout
        android:layout_width="70dp"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginLeft="10dip"
            android:src="@mipmap/icon" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:src="@mipmap/delete_button" />
        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textColor="@android:color/white"
            android:singleLine="true"
            android:textSize="10sp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:layout_below="@id/icon"
            android:text="name"/>
    </RelativeLayout>
</LinearLayout>

The output is

enter image description here

When I sure the display is correct, I separate the item to another xml file

mainactivity.xml

<LinearLayout
    android:id="@+id/list"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="20dp"
    android:layout_weight=".7" >
</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="70dp"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginLeft="10dip"
        android:src="@mipmap/icon" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:src="@mipmap/delete_button" />
    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="@android:color/white"
        android:singleLine="true"
        android:textSize="10sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:layout_below="@id/icon"
        android:text="name"/>
</RelativeLayout>

And then add item in onCreate of mainactivity

LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.item, null, true);
list.addView(view); //(R.id.list), LinearLayout list

Now the output is

enter image description here

And even I add many view to this linearlayout, still only one view can be added into layout

LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.item, null, true);
View view2=inflater.inflate(R.layout.other_item, null, true);
list.addView(view); //(R.id.list), LinearLayout list
list.addView(view2);

What is correct way to add view to layout?

CL So
  • 3,647
  • 10
  • 51
  • 95

1 Answers1

1

Try this instead:

View view = inflater.inflate(R.layout.item, list, false);
list.addView(view); //(R.id.list), LinearLayout list

Explanation:

The second argument to inflate() is the intended parent of the view to be inflated. When you pass null as the second argument, the inflated view does not get any LayoutParams because inflate() doesn't know what the parent eventually will be and thus cannot create the appropriate LayoutParams (almost every ViewGroup defines its own LayoutParams subclass).

When you call addView(), the LinearLayout checks if the child has LayoutParams and whether they are of the appropriate type. If not, it generates some default LayoutParams to set on the view being added. Whatever default it gives the child is causing the unexpected behavior.

In short, the solution is to pass list instead of null when you call inflate().

Karakuri
  • 38,365
  • 12
  • 84
  • 104