0

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.

basickarl
  • 37,187
  • 64
  • 214
  • 335

1 Answers1

0

This seems to be a repeat,all you need to do is specify the parent view instead of added it later to the parent. Hope this helps. See Layout params of loaded view are ignored

Community
  • 1
  • 1
humblerookie
  • 4,717
  • 4
  • 25
  • 40