Layout Resource官方教程(3)在layout中用include嵌入其它layout

简介

<include>Includes a layout file into this layout.

类似 #include ,把layout展开在include处

attributes:

layoutLayout resourceRequired. Reference to a layout resource.
android:idResource ID. Overrides the ID given to the root view in the included layout.
android:layout_heightDimension or keyword. Overrides the height given to the root view in the included layout. Only effective if android:layout_width is also declared.
android:layout_widthDimension or keyword. Overrides the width given to the root view in the included layout. Only effective if android:layout_height is also declared.

  You can include any other layout attributes in the <include> that are supported by the root element in the included layout and they will override those defined in the root element.

注意,findViewById()都是父容器找子控件,被include的 layout里的控件要在都位于主layout中,所以要用 主layout对应的view.findViewById()才能找到子layout中的控件。

Caution: 

  If you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.

  Another way to include a layout is to use ViewStub. It is a lightweight View that consumes no layout space until you explicitly inflate it, at which point, it includes a layout file defined by itsandroid:layout attribute. For more information about using ViewStub, read Loading Views On Demand.

示例:

主layout: frgmt_main.xml

 1   <FrameLayout
 2                 android:id="@android:id/tabcontent"
 3                 android:layout_width="match_parent"
 4                 android:layout_height="match_parent">
 5                 
 6                 <!-- tab1,用include可以引用别处的layout -->
 7                 <include
 8                     android:id="@+id/tab_weixin"
 9                     
10                     android:layout_width="wrap_content"
11                     android:layout_height="wrap_content"
12                     layout="@layout/tab_weixin_layout" />

被include的layout :  tab_weixin_layout.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:id="@+id/tab_weixin_layout"
 6     android:orientation="vertical" >
 7     <LinearLayout
 8         android:id="@+id/tab_weixin_qq_friend"
 9         android:layout_width="match_parent"
10         android:orientation="horizontal"
11         android:layout_height="wrap_content" >
12 
13         <ImageView
14             android:id="@+id/iv_tab_weixin_qq_friend"
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:scaleType="centerInside"
18             android:src="@drawable/tab_weixin_qq_scale" />
19 
20         <TextView
21             android:id="@+id/tv_tab_weixin_qq_friend"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:text="@string/find_qq_friends" />
25 
26     </LinearLayout>
27 
28     <ListView
29         android:id="@+id/tab_weixin_list"
30         android:layout_width="match_parent"
31         android:layout_height="match_parent" >
32     </ListView>
33 
34 </LinearLayout>

 

转载于:https://www.cnblogs.com/sjjg/p/4905847.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值