Android开发学习笔记:5大布局方式详解

Android中常用的5大布局方式有以下几种:

  • 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
  • 帧布局(FrameLayout):组件从屏幕左上方布局组件。
  • 表格布局(TableLayout):按照行列方式布局组件。
  • 相对布局(RelativeLayout):相对其它组件的布局方式。
  •  绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。
 
1. 线性布局

线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。

常用的属性:

android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小

第一个实例

效果图:

 

核心代码如下:

main.xml

	
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     >       
  7.     <LinearLayout 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:orientation="vertical" 
  11.         > 
  12.         <EditText 
  13.             android:layout_width="fill_parent" 
  14.             android:layout_height="wrap_content" 
  15.             /> 
  16.     </LinearLayout> 
  17.     <LinearLayout 
  18.         android:layout_width="fill_parent" 
  19.         android:layout_height="wrap_content" 
  20.         android:orientation="horizontal" 
  21.         android:gravity="right" 
  22.         > 
  23.     <!-- android:gravity="right"表示Button组件向右对齐 --> 
  24.         <Button 
  25.             android:layout_height="wrap_content" 
  26.             android:layout_width="wrap_content" 
  27.             android:text="确定" 
  28.             /> 
  29.         <Button 
  30.             android:layout_height="wrap_content" 
  31.             android:layout_width="wrap_content" 
  32.             android:text="取消" 
  33.             />    
  34.      </LinearLayout> 
  35. </LinearLayout> 

第二个实例

效果图:

 

 

核心代码:

mian.xml
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.  
  6.     <LinearLayout 
  7.     android:orientation="horizontal" 
  8.     android:layout_width="fill_parent" 
  9.     android:layout_height="fill_parent" 
  10.     android:layout_weight="1"> 
  11.       
  12.     <TextView 
  13.         android:text="red" 
  14.         android:gravity="center_horizontal" 
  15.         android:background="#aa0000" 
  16.         android:layout_width="wrap_content" 
  17.         android:layout_height="fill_parent" 
  18.         android:layout_weight="1" 
  19.         /> 
  20.      <!--android:gravity="center_horizontal"水平居中 -->   
  21.      <!--layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。  
  22.          线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。  
  23.         例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,  
  24.         那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;  
  25.         对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,  
  26.         再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度--> 
  27.     <TextView 
  28.         android:text="Teal" 
  29.         android:gravity="center_horizontal" 
  30.         android:background="#008080" 
  31.         android:layout_width="wrap_content" 
  32.         android:layout_height="fill_parent" 
  33.         android:layout_weight="1"/> 
  34.       
  35.     <TextView 
  36.         android:text="blue" 
  37.         android:gravity="center_horizontal" 
  38.         android:background="#0000aa" 
  39.         android:layout_width="wrap_content" 
  40.         android:layout_height="fill_parent" 
  41.         android:layout_weight="1" 
  42.         /> 
  43.       
  44.     <TextView 
  45.         android:text="orange" 
  46.         android:gravity="center_horizontal" 
  47.         android:background="#FFA500" 
  48.         android:layout_width="wrap_content" 
  49.         android:layout_height="fill_parent" 
  50.         android:layout_weight="1" 
  51.         /> 
  52.           
  53.     </LinearLayout>   
  54.     <LinearLayout 
  55.     android:orientation="vertical" 
  56.     android:layout_width="fill_parent" 
  57.     android:layout_height="fill_parent" 
  58.     android:layout_weight="1"> 
  59.       
  60.     <TextView 
  61.         android:text="row one" 

北京卓为科技有限公司(www.draway.com)是一家专业的信息技术服务提供商。我们始终秉承“追求卓越,大有作为”的理念,专注于为客户提供专业的信息技术服务,为职员提供优越的工作条件,为社会提供力所能及的贡献!

公司创始人拥有清华大学工商管理硕士学位,曾经留学美国,后回国创业。公司核心团队拥有丰富的互联网、移动互联网及智能设备研发经验,技术能力涵盖手机端、服务端、大数据、云计算、物联网、车联网、虚拟现实VR/AR等。

公司成立于2012年,目前技术团队拥有软硬件开发、测试工程师50余人,核心管理团队拥有近10年的软件技术服务经验。公司是阿里巴巴、小米科技、阳光保险、天宇手机、CNTV等多家知名客户的签约技术服务供应商,并获得高新企业和“双软”资格认证。

梦想是要有的,万一实现了呢?可是,光有梦想是不行的,更要有行动!如果你对人生充满梦想,对自己的能力充满信心,而且愿意脚踏实地、不辞辛劳地为自己的梦想付出努力!那么欢迎你来卓为试试!

简历请发送:hr@draway.com

电话:010-62974030

邮箱:contact@draway.com

地址:北京市海淀区上地科实大厦D座8层

免费获取报价

姓名

联系方式

产品介绍

注:提交后,我方客户经理会与您电话联系,并提供初步报价
提交成功

提交成功,我们将尽快与您联系!