Android布局管理器的嵌套

布局管理器的嵌套就是将多种布局管理器混合使用,以达到复杂布局的排版效果。如果一个布局页面效果复杂,可能使用一种布局管理器无法完成,那么我们就需要将多种布局管理器嵌套起来以达到显示效果。在Web开发中,编写的CSS基本都是设置嵌套元素的样式的,这个理念是类似的。 
    几种布局管理器都已经介绍过了,我们直接在Eclipse中建立新的项目来说明: 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:id="@+id/txt"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:gravity="center"  
  11.         android:text="文字提示信息" />  
  12.     <LinearLayout  
  13.   xmlns:android="http://schemas.android.com/apk/res/android"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:gravity="center"  
  17.         android:orientation="horizontal" >  
  18.         <ImageView  
  19.             android:id="@+id/img1"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:src="@drawable/ic_launcher" />  
  23.         <ImageView  
  24.             android:id="@+id/img2"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:src="@drawable/ic_launcher" />  
  28.         <ImageView  
  29.             android:id="@+id/img3"  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:src="@drawable/ic_launcher" />  
  33.     </LinearLayout>  
  34. </LinearLayout>  

    相对以前的代码,这个有些复杂,我们一层一层来看,首先外部是个线性布局(该布局管理器垂直放置各个元素),放置了一个文本显示组件TextView,并且设置这个组件为居中显示。之后嵌套了一个线性布局管理器(该布局管理器设置水平放置各个元素),嵌套的布局管理器中放置了三幅图片,那么运行该布局管理器来看看显示效果: 

 

    下面我们继续嵌套,在内部线性布局管理器之下,我们平行放置一个表格布局管理器: 
Xml代码   收藏代码
  1. <TableLayout  
  2.  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:gravity="center"  
  6.     android:orientation="horizontal" >  
  7.     <TableRow>  
  8.         <EditText  
  9.             android:id="@+id/edit"  
  10.             android:layout_width="wrap_content"  
  11.             android:layout_height="wrap_content"  
  12.             android:text="输入关键字进行搜索" />  
  13.         <Button  
  14.             android:id="@+id/btn"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="搜索" />  
  18.     </TableRow>  
  19. </TableLayout>  

    这里面也没有什么可多说的,直接运行程序,我们可以看到: 

 

    只是在嵌套布局管理器时我们要注意设置各个布局管理器的layout_height为包裹高度,而不能设置成为填充屏幕,否则该布局管理器就会占据剩余屏幕整个内容,而其它的布局管理器将无法显示。 
    在嵌套布局管理器时,页面的排版已经非常的复杂了,而若想使用Java代码对其进行控制,这个难度就已经很大了,所以在复杂布局管理器下不建议再编写Java代码来控制了,使用XML文件来布局是比较简单的方式。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值