2013-10-05 65 views
4

我有一个公共布局(common.xml),我想多次包含在另一个布局(layout_a.xml)中。但它只显示了我一次。为什么?多次包含相同的布局

common.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="@android:drawable/alert_light_frame"> 

     <ImageView 

      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:id="@+id/imageView" 

      android:src="@drawable/test" 

      android:scaleType="centerCrop" /> 

     <TextView 

      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:layout_below="@+id/imageView" 
      android:id="@+id/textView" 
      android:text="test" 
      android:layout_marginTop="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginLeft="10dp" /> 
    </RelativeLayout> 
</merge> 

layout_a.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include layout="@layout/common" /> 

    <include layout="@layout/common" /> 
</LinearLayout> 

回答

8

定义在XML中必须是唯一的,当的ID。您包含两个包含相同ID的视图的布局。

Here是你如何去解决它。

p.s.除非您的第一个布局文件中没有包含更多代码,否则该合并标记是无用的。

5

由于btse表示,XML中的ID必须是唯一的。 它可以通过这种方式来实现:

<include android:id="@+id/common1" 
    layout="@layout/common" /> 

<include android:id="@+id/common2" 
    layout="@layout/common" /> 

有关如何访问这两个观点包括里面的元素的信息,你可以检查出this博客文章。

+0

链接不工作.. – Micle

0

我修改了RelativeLayout的layout_height为250dp,因为它们是重叠的。

相关问题