【问题标题】:Android: include a xml into an other xmlAndroid:将一个 xml 包含到另一个 xml 中
【发布时间】:2011-06-10 13:07:51
【问题描述】:

如何将一个 xml 数据包含到另一个 xml 数据中?

我的应用程序有一个标头 xml 文件,我想在我的特殊其他内容 xml 中使用它。有没有办法将标题包含到我的内容中?

【问题讨论】:

    标签: android xml android-xml


    【解决方案1】:

    使用包含标签

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"     
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
     >
      <!-- Header -->
      <include
        android:id="@+id/container_header_lyt"  
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_above=...
        android:layout_toLeftOf=...
        layout="@layout/header_logo_lyt" //Name of the xml layout file you want to include
        />     
    
    ...
    
    </RelativeLayout>
    

    【讨论】:

    • 我没有尝试过RelativeLayout,但应该可以。可能您必须在 include 标记中设置设置它必须放置的属性:android:layout_above、android:layout_toLeftOf 等...
    • 它不适用于我的真实布局可能我的错误是在其他地方我的代码不正确
    • 我已经编辑了答案以使其适应RelativeLayout案例
    • RelativeLayout 不允许 match_parent
    • 使用 fill_parent 代替。我粘贴了我的代码,我正在使用蜂窝,这就是我使用 match_parent 的原因。但它应该与 fill_parent 一起使用。玩一下你的代码。
    【解决方案2】:

    您必须使用 标签声明您的“body”xml 布局,才能在您的主要布局上使用 标签。

    <?xml version="1.0" encoding="utf-8"?>
    <merge>
        <ImageView android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:scaleType="center"
            android:src="@drawable/image" / >
    
        <TextView android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center_horizontal|bottom"
            android:background="#AA000000"
            android:textColor="#ffffffff"
            android:text="Some Text" / >
    </merge>
    

    这是你的 标签的内容

    【讨论】:

      【解决方案3】:

      你应该使用标签:Re-using Layouts with

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-11
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多