I have a simple view:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/contact_selected" android:layout_marginTop="10dp" android:layout_marginEnd="5dp" android:orientation="horizontal" android:padding="3dp"> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Billy Bob" /> </LinearLayout>
When I statically copy LinearLayout markup into my main activity layout, the fields are as expected. However, when I add a view to the main activity layout dynamically, the fields are ignored. This is how I insert the view
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.test, null); TextView txt_title = (TextView)view.findViewById(R.id.txt_title); txt_title.setText("Dynamic #1"); llayout.addView(view, llayout.getChildCount()-1); View view2 = inflater.inflate(R.layout.test, null); txt_title = (TextView)view2.findViewById(R.id.txt_title); txt_title.setText("Dynamic #2"); llayout.addView(view2, llayout.getChildCount()-1);
Here's what it looks like:

The container in the main layout is a LinearLayout, which is a child of the HorizontalScrollView. Any insight appreciated.
android dynamic margins
John ward
source share