How to embed one layout inside another in my case?

﹥>﹥吖頭↗ 提交于 2019-12-09 17:04:52

问题


If I have a layout called bottom.xml,

bottom.xml:(simply contain a textview and edit text view)

 <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>

Is there any way to embed the above bottom.xml layout inside other layouts instead of repeatly writing the same code in several layout files (when other layouts have a part which contains the same layout as bottom.xml)?

For example, if my admin.xml layout also contain part of the layout which looks exactly the same as bottom.xml, how to just embed the bottom.xml inside admin.xml instead of writing the same code again?

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>

If there is no way to do it in Android, what could be the workaround??

----------Update-----------

Like @xevincent suggested, I can reuse the bottom.xml by use <include> tag,

But How to change the id of the elements inside the resued layout?

For example, insdie bottom.xml, I would like to change the id of <editText android:id="@+id/name"> to <editText android:id="@+id/other_name"> when I reuse the bottom.xml layout in other layout, how to change the id ?


回答1:


See this doc reusing layouts.




回答2:


Just upvote xevincent's anwser. I added this answer because SO recommends to "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline."

So, basically, his link explains that you should use <include />.

<com.android.launcher.Workspace
    android:id="@+id/workspace"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    launcher:defaultScreen="1">

    <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
    <include android:id="@+id/cell2" layout="@layout/workspace_screen" />
    <include android:id="@+id/cell3" layout="@layout/workspace_screen" />

</com.android.launcher.Workspace>

And know that you can override the layout parameters:

<include android:layout_width="fill_parent" layout="@layout/image_holder" />



回答3:


Have a look on this doc, Link updated

http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html



来源:https://stackoverflow.com/questions/6621400/how-to-embed-one-layout-inside-another-in-my-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!