I am trying to programmatically add. Here is the parent view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
</LinearLayout>
Here is the view to add.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true" >
<TextView
android:id="@+id/textview"
android:text="TEST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is the code.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.test, container, false);
LinearLayout linear = (LinearLayout) view.findViewById(R.id.test);
RelativeLayout layout;
ImageView image;
TextView text;
LayoutParams lp;
layout = (RelativeLayout) inflater.inflate(R.layout.test_row, null, false);
linear.addView(layout);
return view;
}
It adds the view, but it doesn't center and it also ignores the topmargin property. What am I doing wrong?
Answer: I replaced null when inflating my view with container, did the trick.