问题描述
|
我有以下ListView的工作xml布局
<TextView
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:id=\"@+id/text01\"/>
<RelativeLayout
android:id=\"@+id/rel01\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\">
<ImageView
android:id=\"@+id/widget01\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:src=\"@drawable/logo\"
/>
<ImageButton
android:id=\"@+id/widget02\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_toRightOf=\"@+id/widget01\"
android:src=\"@drawable/refresh\"
/>
</RelativeLayout>
<ListView
android:id=\"@+id/android:list\"
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"/>
<TextView
android:id=\"@+id/android:empty\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:textColor=\"#ffffff\"
/>
</LinearLayout>
我想将text01和所有RelativeLayout移动到另一个文件中(这是我所有活动的标题),并将其包含在此xml中。我正在尝试但无法执行此操作,请问有人可以帮助我吗?
解决方法
您应该将要重用的内容放入其他文件中。然后,您可以使用以下部分:
<!-- my_header.xml -->
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<TextView
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:id=\"@+id/text01\"/>
在另一个文件中包括:
<include layout=\"@layout/my_header\" />
<!-- your other stuff -->
, 首先:创建一个新的文件布局xml:header.xml;它将包含TextView和所有RelativeLayout
第二:您可以使用以下代码在任意位置添加它:
<include android:id=\"@+id/myHeader\"
layout=\"@layout/header\" />