2012-08-14 83 views
33

我有一个与文章的看法。它使用“包含”,我试图在它们之间留出一点空白。但是,“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> 

回答

61

你应该在include标签添加android:layout_widthandroid:layout_height属性。否则,不考虑利润率。

但是,如果要覆盖使用<include>标签布局属性,您必须为其他布局属性生效,同时重写android:layout_heightandroid:layout_width

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

+2

+1你的把戏工作正常 – 2012-12-16 11:47:03

+2

陌生,但真正..日Thnx – 2013-09-12 07:48:10

+1

这只能如果包含的XML本身不具备合并-tag作为根元素,请参阅下面的答案。 – 2015-07-23 16:17:13

7

include标签支持以下属性:

  1. 任何Android:layout_*属性,你可以覆盖。

  2. android:id属性。

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

等: 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-layouts.html

+0

谢谢你的链接,对我的答案非常有帮助! Upvoted您的答案! – 2014-06-24 12:38:08

7

我有同样的问题,并从假面Goranchev答案对我不起作用。

我已经使用ADT的功能“Extract include ...”从布局编辑器中提取一些常用徽章作为TextView元素列表。所以Extract-include-tool将我的TextView-Elements包装在一个合并标签中,这通常很好。

但是,根据来自boiledwater的非常有用的源代码链接,我在第888行https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L888中看到,如果include没有将merge-tag作为其根目录,则仅仅解析include-tag本身的布局属性-元件。

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

1

另一种解决方案是添加Spaceinclude

<Space 
     android:layout_height="8dp" 
     android:layout_width="match_parent" />