android - Why using LayoutInflater is not same to xml? -
i created 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
when sure display correct, separate item 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 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 output is
and add many view linearlayout, still 1 view can added 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 correct way add view layout?
try instead:
view view = inflater.inflate(r.layout.item, list, false); list.addview(view); //(r.id.list), linearlayout list
explanation:
the second argument inflate()
intended parent of view inflated. when pass null
second argument, inflated view not layoutparams
because inflate()
doesn't know parent , cannot create appropriate layoutparams
(almost every viewgroup
defines own layoutparams
subclass).
when call addview()
, linearlayout
checks if child has layoutparams
, whether of appropriate type. if not, generates default layoutparams
set on view being added. whatever default gives child causing unexpected behavior.
in short, solution pass list
instead of null
when call inflate()
.
Comments
Post a Comment