Android的,如何将XML文件转换为另一个XML文件?
问题描述:
我有一个header_bar.xml文件,其code是:
I have a header_bar.xml file that its code is:
<?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文件。我写了这个code:
Now, I want to add it to another XML file. I have written this code:
<?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>
编译器显示我下面的错误在错误日志中:您必须specifiy有效的布局参考。布局ID @布局/ header_bar是无效的。
我惊讶,为什么它不工作。我检查R档,并有header_bar在R.layout。
I amazed why it doesn't work. I checked R file and it had header_bar in its R.layout .
任何建议AP preciated。谢谢
Any suggestion appreciated. Thanks
======================= 更新
======================= Update
我刚刚重新启动Eclipse和现在它的确定。跆拳道是这个编译器?
I just restarted the Eclipse and now its ok. wtf is this compiler?
答
中设置一个XML布局(假设header_bar)后,试试这个:
Try this after setting one xml layout (suppose header_bar) :
LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.main, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));