I've a simple task - add child views to a LinearLayout dynamically, but I don't know, how to do it in a proper way.
There are two ways:
1. Inflate view and passed parent container to the inflate method.
View view = LayoutInflater.from(getActivity()).inflate(R.layout.item, container, true);
At this case, when I've more than one child, LayoutInfater returns the same child view object every time, when inflate it. So I can proper initialized other child views.
2. The next way is to use LinearLayout method addView(View view).
The problem is, that the child view lose it's LayoutParams state. And I must set new LayoutParams for child programmatically. It's a not good practice.
Also we can put child in the complimentary wrapper layout in his own layout resource file. But it's also a not good practice put layout to the wrapper.
My child resource:
<RelativeLayout android:gravity="center_vertical"
android:paddingLeft="25dp"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height">
<TextView android:id="@+id/tv_name"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
So what is the proper way to add child to LinearLayout, without adding LayoutParams programmatically and without wrapper layout?
Thank's a lot for help!