【发布时间】:2011-10-01 01:45:48
【问题描述】:
如果我有一个名为 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 中,而不是编写还是一样的代码?
admin.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 中无法做到这一点,有什么解决方法??
---------更新-----------
就像@xevincent 建议的那样,我可以通过使用<include> 标签来重用bottom.xml,
但是如何更改重新布局中元素的 id 呢?
比如insdie bottom.xml,我想在复用的时候把<editText android:id="@+id/name">的id改成<editText android:id="@+id/other_name"> bottom.xml 其他布局中的布局,如何修改id?
【问题讨论】:
-
“更新”的可能解决方案:stackoverflow.com/questions/3421864/…
标签: android android-layout android-emulator android-widget android-manifest