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" 
  62.         android:textSize="15pt" 
  63.         android:background="#aa0000" 
  64.         android:layout_width="fill_parent" 
  65.         android:layout_height="wrap_content" 
  66.         android:layout_weight="1" 
  67.         /> 
  68.     <!--  -->   
  69.     <TextView 
  70.         android:text="row two" 
  71.         android:textSize="15pt" 
  72.         android:background="#DDA0DD" 
  73.         android:layout_width="fill_parent" 
  74.         android:layout_height="wrap_content" 
  75.         android:layout_weight="1" 
  76.         /> 
  77.       
  78.     <TextView 
  79.         android:text="row three" 
  80.         android:textSize="15pt" 
  81.         android:background="#008080" 
  82.         android:layout_width="fill_parent" 
  83.         android:layout_height="wrap_content" 
  84.         android:layout_weight="1" 
  85.         />    
  86.     <TextView 
  87.         android:text="row four" 
  88.         android:textSize="15pt" 
  89.         android:background="#FFA500" 
  90.         android:layout_width="fill_parent" 
  91.         android:layout_height="wrap_content" 
  92.         android:layout_weight="1" 
  93.         />       
  94.     </LinearLayout>   
  95. </LinearLayout> 
2.  帧布局
帧布局是从屏幕的左上角( 0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
 
 简单的例子
效果图:
 
 核心代码:
main.xml
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     > 
  6.     <TextView    
  7.         android:layout_width="300dp"   
  8.         android:layout_height="300dp"   
  9.         android:background="#00BFFF"          
  10.         /> 
  11.     <TextView    
  12.         android:layout_width="260dp"   
  13.         android:layout_height="260dp"   
  14.         android:background="#FFC0CB"          
  15.         /> 
  16.     <TextView    
  17.         android:layout_width="220dp"   
  18.         android:layout_height="220dp"   
  19.         android:background="#0000FF"          
  20.         /> 
  21. </FrameLayout> 
 
3. 表格布局
表格布局是一个 ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数
 
简单的列子:
效果图:
 
 核心代码:
 main.xml
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     > 
  6.     <TableRow> 
  7.         <Button 
  8.             android:text="Button1" 
  9.             /> 
  10.         <Button 
  11.             android:text="Button2" 
  12.             /> 
  13.         <Button 
  14.             android:text="Button3" 
  15.             /> 
  16.     </TableRow> 
  17.     <TableRow> 
  18.         <Button 
  19.             android:text="Button4" 
  20.             /> 
  21.         <Button 
  22.             android:layout_span="2" 
  23.             android:text="Button5" 
  24.             /> 
  25.     </TableRow> 
  26.       
  27. </TableLayout> 
 
4. 相对布局
相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。
相对布局常用属性请参考我博客的: http://liangruijun.blog.51cto.com/3061169/631816
 
简单的例子
效果图:
 
 核心代码:
main.xml
 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="wrap_content" 
  5.     android:padding="10px" 
  6.     > 
  7.     <TextView    
  8.         android:id="@+id/tev1" 
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content"   
  11.         android:layout_marginBottom="30dp" 
  12.         android:text="Please Type Here:" 
  13.         /> 
  14.     <EditText 
  15.         android:id="@+id/tx1" 
  16.         android:layout_width="match_parent" 
  17.         android:layout_height="wrap_content" 
  18.         android:layout_below="@id/tev1" 
  19.         /> 
  20.     <Button 
  21.         android:id="@+id/btn1" 
  22.         android:layout_height="wrap_content" 
  23.         android:layout_width="wrap_content" 
  24.         android:layout_below="@id/tx1" 
  25.         android:layout_alignParentRight="true" 
  26.         android:text="确定" 
  27.         /> 
  28.     <Button 
  29.         android:id="@+id/btn2" 
  30.         android:layout_height="wrap_content" 
  31.         android:layout_width="wrap_content" 
  32.         android:layout_below="@id/tx1" 
  33.         android:layout_toLeftOf="@id/btn1" 
  34.         android:layout_marginRight="30dp" 
  35.         android:text="取消" 
  36.         /> 
  37. </RelativeLayout> 
5.  绝对布局
 绝对布局通过指定子组件的确切 X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。

本文转自 lingdududu 51CTO博客,原文链接: http://blog.51cto.com/liangruijun/632532

 

相关文章
|
2天前
|
测试技术 Linux Android开发
探索安卓开发之旅:从初学者到专家
【8月更文挑战第29天】本文是一篇为初学者和有一定经验的开发者准备的安卓开发指南。我们将从基础概念开始,逐步深入到高级主题,如自定义视图、性能优化等。无论你是刚刚入门,还是希望提升自己的技能,这篇文章都将为你提供有价值的信息和建议。让我们一起踏上这段激动人心的旅程吧!
|
1天前
|
供应链 物联网 区块链
未来触手可及:探索新兴技术的趋势与应用安卓开发中的自定义视图:从基础到进阶
【8月更文挑战第30天】随着科技的飞速发展,新兴技术如区块链、物联网和虚拟现实正在重塑我们的世界。本文将深入探讨这些技术的发展趋势和应用场景,带你领略未来的可能性。
|
2天前
|
存储 搜索推荐 Java
探索安卓开发中的自定义视图:打造个性化UI组件Java中的异常处理:从基础到高级
【8月更文挑战第29天】在安卓应用的海洋中,一个独特的用户界面(UI)能让应用脱颖而出。自定义视图是实现这一目标的强大工具。本文将通过一个简单的自定义计数器视图示例,展示如何从零开始创建一个具有独特风格和功能的安卓UI组件,并讨论在此过程中涉及的设计原则、性能优化和兼容性问题。准备好让你的应用与众不同了吗?让我们开始吧!
|
1天前
|
XML 搜索推荐 Android开发
安卓开发中的自定义View组件实践
【8月更文挑战第30天】探索Android世界,自定义View是提升应用界面的关键。本文以简洁的语言带你了解如何创建自定义View,从基础到高级技巧,一步步打造个性化的UI组件。
|
2天前
|
设计模式 Java Android开发
探索安卓应用开发:从新手到专家的旅程探索iOS开发中的SwiftUI框架
【8月更文挑战第29天】本文旨在通过一个易于理解的旅程比喻,带领读者深入探讨安卓应用开发的各个方面。我们将从基础概念入手,逐步过渡到高级技术,最后讨论如何维护和推广你的应用。无论你是编程新手还是有经验的开发者,这篇文章都将为你提供有价值的见解和实用的代码示例。让我们一起开始这段激动人心的旅程吧!
|
1天前
|
JSON 缓存 搜索推荐
探索安卓开发:打造个性化天气应用探索移动应用开发之旅:从基础到高级
【8月更文挑战第30天】在数字化时代,智能手机已成为我们日常生活中不可或缺的一部分。安卓系统以其开放性和灵活性赢得了全球用户的青睐。本文将引导你了解如何利用安卓开发技术,从零开始构建一个具有个性化特色的天气应用。我们将一起深入探讨应用的设计思路、核心功能实现以及用户交互体验的优化方法。无论你是编程新手还是有一定经验的开发者,这篇文章都将为你提供有价值的指导和灵感。
|
1天前
|
安全 网络安全 数据安全/隐私保护
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享安卓与iOS开发中的线程管理比较
【8月更文挑战第30天】本文将探讨网络安全与信息安全的重要性,并分享关于网络安全漏洞、加密技术和安全意识的知识。我们将了解常见的网络攻击类型和防御策略,以及如何通过加密技术和提高安全意识来保护个人和组织的信息安全。
|
1天前
|
IDE Java Linux
探索安卓开发:从基础到进阶的旅程Java中的异常处理:从基础到高级
【8月更文挑战第30天】在这个数字时代,移动应用已经成为我们日常生活中不可或缺的一部分。安卓系统由于其开放性和灵活性,成为了开发者的首选平台之一。本文将带领读者踏上一段从零开始的安卓开发之旅,通过深入浅出的方式介绍安卓开发的基础知识、核心概念以及进阶技巧。我们将一起构建一个简单的安卓应用,并探讨如何优化代码以提高性能和应用的用户体验。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供宝贵的知识和启发。
|
1天前
|
Android开发 Swift iOS开发
探索Android和iOS开发的差异性与互补性深入浅出Python装饰器
【8月更文挑战第30天】在移动应用开发的广阔天地中,Android和iOS两大平台以其独特的魅力和技术架构引领潮流。本文将深入探讨这两个平台的开发环境、编程语言和用户界面设计等方面的不同之处,并揭示它们之间的互补性。通过比较分析,我们将发现每个平台的核心优势,以及如何将这些优势融合到跨平台开发策略中,为开发者提供全面的视角和实用的建议。
|
1天前
|
物联网 区块链 vr&ar
未来已来:探索区块链、物联网与虚拟现实技术的融合与应用安卓与iOS开发中的跨平台框架选择
【8月更文挑战第30天】在科技的巨轮下,新技术不断涌现,引领着社会进步。本文将聚焦于当前最前沿的技术——区块链、物联网和虚拟现实,探讨它们各自的发展趋势及其在未来可能的应用场景。我们将从这些技术的基本定义出发,逐步深入到它们的相互作用和集成应用,最后展望它们如何共同塑造一个全新的数字生态系统。