android,如何将XML文件包含到另一个XML文件中?

时间:2012-03-09 02:15:52

标签: android xml include

我有一个header_bar.xml文件,其代码是:

<?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 = "wrap_content"
    android:background    = "@drawable/bar_top"
    android:layout_alignParentTop = "true" >


    <Button
        android:id                      = "@+id/btn_menu_settings"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_alignParentRight = "true"
        android:layout_centerVertical   = "true"
        android:background              = "@drawable/btn_header_settings"
        android:text                    = "@string/menu_btnSetting"
        android:layout_marginRight      = "20dip"
        android:textColor               = "#F9F9F6" />

    <!-- The text of this button depends of which activity we are, may be change to "Back" or "Menu" -->
    <Button
        android:id                      = "@+id/btn_menu_compound"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_alignParentLeft  = "true"
        android:layout_centerVertical   = "true"
        android:background              = "@drawable/btn_header_menu"
        android:text                    = "@string/menu_btnMenu"
        android:layout_marginLeft       = "20dip"
        android:textColor               = "#F9F9F6" />


</RelativeLayout>

现在,我想将它添加到另一个XML文件中。我写了这段代码:

<?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"
    android:orientation   = "vertical" >


    <!-- Loading header of this UI which is coded separately -->
    <include
        android:id="@+id/header" 
        layout="@layout/header_bar" />



    <GridView 
        android:id="@+id/news_gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_below="@id/header"
        android:layout_marginTop="25dip"
        android:layout_centerInParent="true"
        android:numColumns="2"
        android:verticalSpacing="5dp"
        android:horizontalSpacing="5dp"
        android:background="@drawable/bg" />  


    <!-- Loading footer of this UI which is coded separately -->
    <include layout="@layout/footer_gallery" /> 

</RelativeLayout>  

编译器在错误日志中显示以下错误:您必须指定有效的布局参考。布局ID @ layout / header_bar无效。

我惊讶为什么它不起作用。我检查了R文件,它的R.layout中有header_bar。

任何建议都表示赞赏。感谢

======================= 的更新

我刚刚重启了Eclipse,现在还可以。 wtf是这个编译器吗?

1 个答案:

答案 0 :(得分:0)

设置一个xml布局后尝试此操作(假设header_bar):

LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.main, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));