首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在布局中包含布局?

如何在布局中包含布局?
EN

Stack Overflow用户
提问于 2011-04-12 15:59:44
回答 3查看 130.7K关注 0票数 104

如何在Android中包含布局?

我正在创建通用布局。我想在另一个页面中包含该布局。

EN

回答 3

Stack Overflow用户

发布于 2014-04-10 03:34:54

请注意,如果将android:id...包含在<include />标记中,它将覆盖在包含的布局中定义的任何id。例如:

代码语言:javascript
复制
<include
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/some_id_if_needed"
   layout="@layout/yourlayout" />

yourlayout.xml:

代码语言:javascript
复制
<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/some_other_id">
   <Button
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button1" />
 </LinearLayout>

然后,您可以在代码中引用此包含的布局,如下所示:

代码语言:javascript
复制
View includedLayout = findViewById(R.id.some_id_if_needed);
Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1);
票数 69
EN

Stack Overflow用户

发布于 2011-04-12 16:04:44

使用<include />标签。

代码语言:javascript
复制
          <include 
            android:id="@+id/some_id_if_needed"
            layout="@layout/some_layout"/>

另外,请阅读Creating Reusable UI ComponentsMerging Layouts文章。

票数 20
EN

Stack Overflow用户

发布于 2014-08-21 15:46:14

尝尝这个

代码语言:javascript
复制
<include
            android:id="@+id/OnlineOffline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            layout="@layout/YourLayoutName" />
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5632114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档