Margin does not impact in "include"

Viewed 18334

I have a view with articles. It uses "include", and I'm trying to make a little margin between them. However, "android:layout_marginTop" does not seem to have any impact on the layout.

What am I doing wrong?

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >    
    <include android:id="@+id/article1" layout="@layout/mainarticle" />
    <include android:id="@+id/article2" android:layout_marginTop="10dip" layout="@layout/article" />
    <include android:id="@+id/article3" android:layout_marginTop="10dip" layout="@layout/article" />
    <include android:id="@+id/article4" android:layout_marginTop="10dip" layout="@layout/article" />
    <include android:id="@+id/article5" android:layout_marginTop="10dip" layout="@layout/article" />    
</LinearLayout>
6 Answers

include tag support below properties:

  1. Any android:layout_* attributes which you can overwrite.

  2. android:id attribute.

  3. layout attribute.
  4. android:visibility attribute.

Etc: include android:id=”@+id/news_title” android:layout_width=”match_parent” android:layout_height=”match_parent” layout=”@layout/title”/>

Please read: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L777
http://developer.android.com/training/improving-layouts/reusing-layouts.html

must insert include into other layout.

e.g. Relativelayout

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp">

        <include
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/sub_edit_new_customer" />

    </RelativeLayout>

In my case I solved the problem by adding some padding.

The layout you want to include:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"> <!-- add padding here -->

        <!-- your custom layout -->

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>
Related