Android的布局方式共有6种,分别是LinearLayout(线性布局)TableLayout(表格布局)FrameLayout(帧布局)RelativeLayout(相对布局)GridLayout(网格布局)以及AbsoluteLayout(绝对布局)。

  本次主要介绍 LinearLayout 线性布局,其余布局留待以后介绍。

  线性布局由 LinearLayout 类来代表, LinearLayout 可以控制各组件横向或纵向排列。

  LinearLayout 常用属性介绍

  1.  android:orientation

    设置布局管理器内组件的排列方式,可设置为 horizon (水平排列)、vertical (垂直排列)。

    

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3"/>
</LinearLayout>

  上述代码设置了三个按钮(Button),第 3 行代码设置其排列方式为 horizon 水平排列,效果图如图 1: 

图 1 :                                                   图 2 :                                                图 3 :

                              

将上述代码的第 3 行中 orientation 的值由 horizon 改为 vertical (如下代码),则按钮的排列方式变为垂直排列 ,如图 2

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3"/>
</LinearLayout>

   注意: Android 的线性布局不会换行,当组件一个挨着一个的排列到头之后,剩下的组件将不会被显示出来

  例:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3"/>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn4"/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn5"/>
</LinearLayout>

上述代码定义了 5 个按钮,使其水平排列,但其效果如上图 3 所示,只显示了四个按钮,第 5 个按钮没有被显示出来。

  2.  android:gravity

    设置布局管理器内组件的对齐方式,该属性值可设为 top(顶部对齐) 、bottom(底部对齐) 、left(左对齐) 、right(右对齐) 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直与水平方向都居中) 、 fill (填充)、  clip_vertical(垂直方向裁剪) 、  clip_horizontal(水平方向裁剪) 。

    可同时指定多种对其方式的组合,中间用“|”连接,如下方代码设置对齐方式为 left|center_vertical 表示出现在屏幕左边且垂直居中,效果图如图 4 。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|center_vertical">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3"/>
</LinearLayout>

   图 4 :

    

除以上两个常用属性外,LinearLayout 的属性还有以下几个:

  android:baselineAligned            该属性设为 false ,将会阻止该布局管理器与它的子元素的基线对其。

  android:divider                设置垂直布局时两个按钮直接的分隔条。

  android:measureWithLargestChild      该属性设为 true 时,所有带权重的子元素都会具有最大子元素的最小尺寸。

最后介绍一下 LinearLayout 子元素支持的常用属性:

  android:layout_gravity    指定该子元素在 LinearLayout 中的对其方式

  android:layout_weight    指定该子元素在 LinearLayout 中所占的权重

以上就是关于 android 布局中 LinearLayout 的介绍,如有疏漏或错误,欢迎指正。

  本文为博主原创文章,转载请在明显位置注明出处:

  原文地址 http://www.cnblogs.com/zzulihao

Android布局管理详解(1)—— LinearLayout 线性布局的更多相关文章

  1. 弹性布局学习-详解flex-wrap(五)

    目录 弹性布局学习-介绍(一)  弹性布局学习-详解 flex-direction[决定主轴的方向](二) 弹性布局学习-详解 justify-content(三) 弹性布局学习-详解 align-i ...

  2. 弹性布局学习-详解 align-items(四)

    目录 弹性布局学习-介绍(一)  弹性布局学习-详解 flex-direction[决定主轴的方向](二) 弹性布局学习-详解 justify-content(三) 弹性布局学习-详解 align-i ...

  3. Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)

    [Android布局学习系列]   1.Android 布局学习之——Layout(布局)详解一   2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)   3.And ...

  4. Android零基础入门第25节:最简单最常用的LinearLayout线性布局

    原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...

  5. Android开发之详解五大布局

    http://bbs.chinaunix.net/thread-3654213-1-1.html 为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是: LinearLayo ...

  6. android 59 LinearLayout 线性布局

    ##常见的布局* LinearLayout 线性布局线性布局往左右拉是拉不动的,> 线性布局的朝向 vertical|horizontal> 线性布局的权重 weight 和 0dip一起 ...

  7. Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件

    UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...

  8. Android layout 布局 属性详解

    第一类:属性值 true或者 false           android:layout_centerHrizontal 水平居中     android:layout_centerVertical ...

  9. 2.2.1 LinearLayout(线性布局)

    本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局), RelativeLayout(相对布局), TableLayout(表格布局) ...

随机推荐

  1. HDU-4861-Couple doubi(数学题,难懂!难懂!)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4861 这个题只能说没弄懂,感觉很难,看博客也看不懂,只能,多看几次,看能不能有所突破了. 代码的话只有 ...

  2. jQuery html text val方法使用

    jQuery html text val方法使用 <%@ page language="java" import="java.util.*" pageEn ...

  3. 负载均衡软件LVS分析二(安装)

    一.  安装LVS软件 1.安装前准备工作操作系统:统一采用Centos4.4版本.地址规划,如表1所示:表1 更详细的信息如图2所示: 图2  LVS DR模式安装部署结构图 图2中的VIP指的是虚 ...

  4. MongoDB基础之五:游标

    1.cursor(游标)是什么 ? 通俗的说,游标不是查询结果,而是查询的返回资源,或者接口. 通过这个接口,你可以逐条读取. 就像php中的fopen打开文件,得到一个资源一样, 通过资源,可以一行 ...

  5. .Net Core 之 MSBuild 介绍

    前言 关于 .NET Core 旧版本的 sdk 介绍可以参看我以前的 这篇 文章. 8 个小时前,.NET Core 项目组释放了 .NET Core 新一轮的 sdk 工具更新,即 RC4 版本 ...

  6. 数据结构(Java描述)之二叉树

    基础概念 二叉树(binary tree)是一棵树,其中每个结点都不能有多于两个儿子. 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于或等于它 ...

  7. openstack安装文档

    #########################################openstack m版本部署安装################################## 控制节点.网络 ...

  8. JavaScript 中的 this 问题总结 !

    2016-12-28 vvv阿城 JavaScript 转自  https://qiutc.me/post/this-this-this-in-javascript.html#call,_apply, ...

  9. phpcms如何做企业站--> 替换首页最初操作

    首先用一个静态首页的模板,通过cms进行替换做成一个有后台的 首页的替换流程首先要先把静态网页做出来,拿这个页面去替换 页面所有的文件都在这,做静态页面的文件 现在要做的是把这些文件复制一下拿到php ...

  10. PropertyChangeSupport的使用

    使用目的 当你需要监听对象属性的变化时,可以使用PropertyChangeSupport类来管理监听器,可以在一些关联属性的场合使用. 使用示例,以下为BetaConfig对象添加了管理属性监听器的 ...