Android: include a xml into an other xml

主宰稳场 提交于 2019-11-26 21:45:54

问题


How to include a xml data into an other xml data

i have a header xml-file for my application wich i want to use in my special other content xmls is there a ways to include the header into my content?


回答1:


use include tag

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



回答2:


You have to declare your "body" xml layout with a < merge > tag to use < include > tag on your principal layout.

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

This is the content of your < include > tag




回答3:


You should use < include > tag: Re-using Layouts with



来源:https://stackoverflow.com/questions/6306791/android-include-a-xml-into-an-other-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!