微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在我的情况下将一个布局嵌入另一个布局?

如果我有一个名为bottom.xml的布局,

bottom.xml :(只包含一个textview和编辑文本视图)

<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中嵌入bottom.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. to< editText android:id =“@ id / other_name”>当我在其他布局中重用bottom.xml布局时,如何更改id?

解决方法

请参阅此文档 reusing layouts.

原文地址:https://www.jb51.cc/android/318285.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐