如果我有一个名为bottom.xml的布局,
bottom.xml:(只包含一个文本视图和编辑文本视图)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="@string/username"
/>
<EditText
android:id="@+id/name"
android:layout_width="120dip"
android:layout_height="50dip"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
是否有任何方法可以将上面的bottom.xml布局嵌入到其他布局中,而不是在多个布局文件中重复编写相同的代码(当其他布局的某个部分包含与bottom.xml相同的布局时)?
例如,如果我的admin.xml布局也包含与bottom.xml完全相同的部分布局,那么如何将bottom.xml嵌入到admin.xml中,而不是再次编写相同的代码?
管理.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
...
...
<!--How to embed bottom.xml here-->
...
</LinearLayout>
如果在android中没有办法做到这一点,那么解决方法是什么??
——————————————————————————更新--
就像@xe文森特建议的那样,我可以通过使用
<include>
标记来重用bottom.xml,但是如何更改恢复的布局中元素的id?
例如,在insdie bottom.xml中,当我在其他布局中重用bottom.xml布局时,我想将
<editText android:id="@+id/name">
的id更改为<editText android:id="@+id/other_name">
,如何更改id? 最佳答案
参见本文档reusing layouts。