51

我对文章有看法。它使用“包含”,我试图在它们之间留出一点余地。但是,“android:layout_marginTop”似乎对布局没有任何影响。

我究竟做错了什么?

<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>
4

6 回答 6

82

您应该在标签中添加android:layout_widthandandroid:layout_height属性。include否则,不考虑边距。

但是,如果您想使用<include>标签覆盖布局属性,则必须同时覆盖android:layout_heightandroid:layout_width才能使其他布局属性生效。

https://developer.android.com/training/improving-layouts/reusing-layouts.html#Include

于 2012-11-29T10:12:18.813 回答
15

我遇到了同样的问题,Kamen Goranchev 的回答对我不起作用。

我从布局编辑器中使用了 ADT 的“提取包含...”功能来提取一些常用的徽章作为 TextView 元素的列表。因此,Extract-include-tool 将我的 TextView-Elements 包装在一个merge-tag中,这通常会很好。

但是,根据我在第 888 行https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L888中看到的来自沸水的非常有用的源代码链接,布局-仅当包含没有合并标记作为其根元素时,才解析包含标记本身的属性。

因此,我从包含中删除了合并标签,并使用了另一个 ViewGroup-标签,例如 FrameLayout。然后包含标签中的边距按预期工作。

于 2014-06-24T12:37:21.130 回答
8

include标签支持以下属性:

  1. layout_*任何可以覆盖的android:属性。

  2. android:id属性。

  3. layout属性。
  4. android:visibility属性。

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

请阅读: https ://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L777
http://developer.android.com/training/improving-layouts/reusing-布局.html

于 2013-11-29T03:02:30.950 回答
6

另一种解决方案是在Space之前添加include

    <Space
        android:layout_height="8dp"
        android:layout_width="match_parent" />
于 2017-05-04T09:45:08.430 回答
2

必须将包含插入到其他布局中。

例如相对布局

<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>
于 2018-08-29T05:27:01.717 回答
0

就我而言,我通过添加一些填充解决了这个问题。

您要包含的布局:

<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>
于 2020-12-14T13:29:33.667 回答