Search code examples
androidandroid-linearlayout

marginTop and marginBottom don't work for LinearLayout


Main(container) LinearLayout:

<LinearLayout
    android:id="@+id/LinearHome"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</LinearLayout>

Child LinearLayouts (I'm adding them dynamically by inflating new one):

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/lnote"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp" />

What's the problem?


Solution

  • Set the second parameter of the inflate() method to the parent (main container)

    LinearLayout lNewNote = (LinearLayout) getLayoutInflater().inflate(R.layout.note_layout, HomeContainer); 
    HomeContainer.addView(lNewNote);
    

    I believe it did the trick for me. (I guess HomeContainer ist an object, not a class)