Android UI详解之布局管理器

一、布局管理器

①顶级父类View

②子类GroupView

③AbsoluteLayout、FrameLayout、LinearLayout、GridLayout、RelativeLayout

④TableLayout 、TabWidget继承LinearLayout

2、在分析布局之前,我们首先看看控件:Android中任何可视化的控件都是从android.veiw.View继承而来的,系统提供了两种方法来设置视图:第一种也是我们最常用的的使用XML文件来配置View的相关属性,然后在程序启动时系统根据配置文件来创建相应的View视图。第二种是我们在代码中直接使用相应的类来创建视图。

如何使用XML文件定义视图:

每个Android项目的源码目录下都有个res/layout目录,这个目录就是用来存放布局文件的。布局文件一般以对应activity的名字命名,以 .xml 为后缀。在xml中为创建组件时,需要为组件指定id,如:android:id="@+id/名字"系统会自动在gen目录下创建相应的R资源类变量。

如何在代码中使用视图:

在代码中创建每个Activity时,一般是在onCreate()方法中,调用setContentView()来加载指定的xml布局文件,然后就可以通过findViewById()来获得在布局文件中创建的相应id的控件了,如Button等

下面我们来介绍Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。这些布局都可以嵌套使用。


(1)LinearLayout 线性布局

线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation="vertical" 和 android:orientation="horizontal"来设置。

android:layout_weight 表示子元素占据的空间大小的比例,有人说这个值大小和占据空间成正比,有人说反比。我在实际应用中设置和网上资料显示的刚好相反,这个问题后面会专门写一篇文章来分析。现在我们只需要按照正比例来设置就可以。

例如下面我们实现一个如图所示的简易计算器界面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".MainActivity" >
// 这里第一行显示标签为一个水平布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/msg"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="">
</EditText>
</LinearLayout>
// 第二行为 mc m+ m- mr 四个Button构成一个水平布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mc" android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="m+" android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="m-" android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mr" android:layout_weight="1">
</Button>
</LinearLayout>
// 同上 C +/-  / * 四个Button构成一个水平布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+/-" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="8" android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="9" android:layout_weight="1">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-" android:layout_weight="1">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" >
</Button>
</LinearLayout>
// 最外层是一个水平布局,由左边上面一行1 2 3三个Button,下面一行的0 . 两个Button 和 右边的=构成
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
// 这里 1 2 3 和 下面的 0 . 构成一个垂直布局
<LinearLayout android:orientation="vertical"
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
// 这里的 1 2 3 构成一个水平布局
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"></Button>
</LinearLayout>
// 这里的 0 和 . 构成一个水平布局,注意这里的android_weight参数设置
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="0"></Button>
<Button
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."></Button>
</LinearLayout>
</LinearLayout>
// 这里一个单独Button构成的垂直布局
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="="></Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>

(2)TableLayout 表格布局

表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线性布局,且所以子元素宽度和高度一致。

注意:在TableLayout中,单元格可以为空,但是不能跨列,意思是只能不能有相邻的单元格为空。

在TableLayout布局中,一列的宽度由该列中最宽的那个单元格指定,而该表格的宽度由父容器指定。可以为每一列设置以下属性:

     Shrinkable  表示该列的宽度可以进行收缩,以使表格能够适应父容器的大小

     Stretchable 表示该列的宽度可以进行拉伸,以使能够填满表格中的空闲空间

     Collapsed  表示该列会被隐藏

TableLayout中的特有属性:

        android:collapseColumns

        android:shrinkColumns ="0,1,2,3" // 表示 4列都可以收缩,就是当有多列时,如果不设置此项会出现溢出屏幕,不会收缩

        android:stretchColumns = "0,1,2,3" // 表示 4列都可以拉伸,就是当只有少量按钮,默认宽度不能占满一横屏,就需要拉伸。

Demo 1     

      这个对应的xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:shrinkColumns="0,1,2,3,4,5"
android:stretchColumns="0,1,2,3,4,5"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:gravity="center"
android:padding="10dp"
android:text="Button1">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button3">
</Button>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:gravity="center"
android:padding="10dp"
android:text="Button4">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button5">
</Button>
</TableRow>
</TableLayout>


Demo 2 隐藏两列

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:collapseColumns="0,1"
android:shrinkColumns="0,1,2,3,4,5"
android:stretchColumns="0,1,2,3,4,5"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:gravity="center"
android:padding="10dp"
android:text="Button1">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button2">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button3">
</Button>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:gravity="center"
android:padding="10dp"
android:text="Button4">
</Button>
<Button android:gravity="center"
android:padding="10dp"
android:text="Button5">
</Button>
</TableRow>
</TableLayout>

(3)RelativeLayout 相对布局

RelativeLayout继承于android.widget.ViewGroup,其按照子元素之间的位置关系完成布局的,作为Android系统五大布局中最灵活也是最常用的一种布局方式,非常适合于一些比较复杂的界面设计。

注意:在引用其他子元素之前,引用的ID必须已经存在,否则将出现异常。

常用的位置属性:

android:layout_toLeftOf        该组件位于引用组件的左方
  android:layout_toRightOf      该组件位于引用组件的右方
  android:layout_above          该组件位于引用组件的上方
  android:layout_below              该组件位于引用组件的下方
  android:layout_alignParentLeft    该组件是否对齐父组件的左端
  android:layout_alignParentRight   该组件是否齐其父组件的右端
  android:layout_alignParentTop     该组件是否对齐父组件的顶部
  android:layout_alignParentBottom      该组件是否对齐父组件的底部
  android:layout_centerInParent     该组件是否相对于父组件居中
  android:layout_centerHorizontal       该组件是否横向居中
  android:layout_centerVertical     该组件是否垂直居中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button1"
></Button>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn1"
android:layout_above="@id/btn1"
android:text="Button2"
></Button>
<Button android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn1"
android:layout_above="@id/btn1"
android:text="Button3"
></Button>
<Button android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn2"
android:layout_toLeftOf="@id/btn3"
android:layout_above="@id/btn2"
android:text="Button4"
></Button>
</RelativeLayout>
android:layout_centerHorizontal:true用于相对布局(RelativeLayout)的子控件居中。
android:gravity="center"用于控件里的内容怎么显示 

(4)FrameLayout 框架布局

FrameLayout是最简单的布局了。所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。

在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义。控件自动的堆放在左上角,根本不听你的控制。

看以下的例子:

第四层"/>

</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50dip"
android:textColor="#ffffff"
android:text="第一层"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dip"
android:textColor="#ffff00"
android:text="第二层"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:textColor="#ff00ff"
android:text="第三层"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#00ffff"
android:text="第四层"/>

效果都是从左上脚开始排列

变化1

我们现在来尝试改变一下他们的位置。把第一个和第二个文本框改成:

<TextView

android:id="@+id/tv1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="50dip"

android:textColor="#ffffff"

android:text="第一层"/>

<TextView

android:id="@+id/tv2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="40dip"

android:textColor="#ffff00"

android:layout_toRightOf="@id/tv1"

android:text="第二层"/>

也就是说,让第二个文本框放在第一个文本框的右边。我们来看看效果。看到了没?还是一样的不变吧。

变化2

我们来尝试下android:gravity属性。把第三个文本框改成:

<TextView

android:id="@+id/tv3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="30dip"

android:textColor="#ff00ff"

android:gravity="right"

android:text="第三层"/>

看看效果如何?会发现 ,竟然没有覆盖,而是错开了!!!

首先呢,我们不要大惊小怪。这个现象并不说明FrameLayout失效了。gravity属性,是控制控件内部文本的格式的。而我们看我们控件的宽的属性是什么?是“fill_parent”,也就是说,我们文本框的宽度就是屏幕的宽度。那么android:gravity="right"文本靠右,而文本框本身还是左上堆叠在一起的。不信,我们再来改改:

<TextView

android:id="@+id/tv3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="30dip"

android:textColor="#ff00ff"

android:gravity="right"

android:text="第三层"/>

我们让第三个文本框的宽度自适应,也就是保证显示全文字即可。这个时候看一下效果呢?是不是打回原形啦?哈哈哈。

变化3

我们再来试试” android:layout_centerVertical”属性。把第四个文本框改成:

<TextView

android:id="@+id/tv4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20dip"

android:textColor="#00ffff"

android:layout_centerVertical="true"

android:text="第四层"/>

效果如何?没任何效果!

总结一下,经过以上的3个实验,我们知道FrameLayout根本无法控制他的子控件的位置。所有的控件都是左上对其。但是控件本身是可以控制自己内部的布局的。所以利用透明,也是可以完成一些简单的功能的。例如屏幕四个角各显示一些文字(是显示文字,没法放置控件)。因为每一层覆盖下一层的时候,如果用透明背景,则下一层不会被背景覆盖。

什么是透明背景?这个……说来话长啦。偷个懒,下次写一下透明的处理。

是不是有人会问,这么简单的Layout有什么用?我想还是有它存在的价值的。

当你需要自己写一个View的时候,在View里面已经完成了你的逻辑(例如游戏^_^),那么这个View只需要一个容器放置,就可以使用FrameLayout了。虽然用其他的布局也可以,但是用最简单的不是更省系统资源么。

(5) AbsoluteLayou 绝对布局

绝对布局中将所有的子元素通过设置android:layout_x 和 android:layout_y属性,将子元素的坐标位置固定下来,即坐标(android:layout_x, android:layout_y) ,layout_x用来表示横坐标,layout_y用来表示纵坐标。 屏幕左上角为坐标(0,0),横向往右为正方,纵向往下为正方。实际应用中,这种布局用的比较少,因为Android终端一般机型比较多,各自的屏幕大小。分辨率等可能都不一样,如果用绝对布局,可能导致在有的终端上显示不全等。

除上面讲过之外常用的几个布局的属性: 
(1)layout_margin    用于设置控件边缘相对于父控件的边距 
android:layout_marginLeft  
android:layout_marginRight 
android:layout_marginTop 
android:layout_marginBottom

(2) layout_padding  
用于设置控件内容相对于控件边缘的边距 
android:layout_paddingLeft 
android:layout_paddingRight 
android:layout_paddingTop 
android:layout_paddingBottom

(3) layout_width/height 
用于设置控件的高度和宽度 
wrap_content 内容包裹,表示这个控件的里面文字大小填充 
fill_parent 跟随父窗口 
match_parent 和fill_parent 一样,
(4)

基中的android:layout_width和android:width的区别:

  • android:layout_width 只有两种选择一个是fill_parent ,二是wrap_content
  • android:width 这个是用来view的具体宽度的,以像素为单位。

android:width 支持

  • px (pixels)像素
  • dip (device independent pixels)设备独立像素
  • sp (scaled pixels ― best for text size)放大像素--对文本大小最好
  • pt (points) 点
  • in (inches)英寸
  • mm (millimeters)毫米

  • 如果view中有layout_width属性的话,不管有没有width属性,该view的形状是由父容器

(5) gravity   
用于设置 View组件里面内容 对齐方式 
top bottom left   right  center等

(6) android:layout_gravity    
用于 设置Container组件的对齐 方式 
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐 
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐 
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐

android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

Android UI详解之布局管理器(一)相关推荐

  1. smss.exe是什么进程?详解Windows会话管理器中的smss.exe

    smss.exe是什么进程?详解Windows会话管理器中的smss.exe 进程综述 smss.exe是什么进程?详解Windows会话管理器中的smss.exe-冯金伟博客园smss.exe是什么 ...

  2. Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout

    目录 布局管理器 RelativeLayout 常见属性 Relative的实践操作(实现软件更新界面) LinearLayout 常见属性 LinearLayout的实践操作(模范登录以及微信底部) ...

  3. Android中的6大布局管理器

    文章目录 6大布局管理器详解 1. LinearLayout 线性布局管理器 2. TableLayout 表格布局管理器 3. GridLayout 网格布局管理器 ~~~~~~~~~~~~~~~~ ...

  4. Linux系统编程3:基础篇之详解Linux软件包管理器yum

    文章目录 (1)什么是软件包 A:软件包 B:注意事项 C:yum基本使用 (2)安装rzsz (1)什么是软件包 A:软件包 区别Windows,在Linux下安装软件,第一种方法是下载程序源代码, ...

  5. 疯狂Android讲义(二)——第二部分:第1组UI组件(布局管理器)

    一.第1组UI组件:布局管理器 Android 的界面组件比较多,不利于掌握它们内在的关系.为了帮助读者更好地掌握Android界面组件的关系,本书将会把这些界面组件按照它们的关联分析,分为几组进行介 ...

  6. Android精讲--界面编程2(布局管理器)

    为什么需要布局管理器 为了更好地管理Android应用的用户界面里的各种组件,Android提供了布局管理器.通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性.通常来说,推荐使 ...

  7. Android开发详解:第3章《App UI 设计》

    第3章:App UI设计 3.1:UI设计的相关概念 3.3:布局管理器 3.4:常用APP UI界面设计 3.1:UI设计的相关概念 View View类在Android中可以理解为视图.它占据屏幕 ...

  8. Android 布局管理器

    布局管理器:控制组件是如何摆放的. 5种常用的布局管理器: RelativeLayout,相对布局管理器 LineraLayout,线性 FrameLayout,帧 TableLayout,表格 Gr ...

  9. QT 常用布局管理器

    Qt布局管理详解 详解 QT 布局管理界面 图文并茂 QT主要布局管理器分类 QHBoxLayout(水平布局) 把子窗口从左到右排列在一个水平行上. QWidget *window = new QW ...

最新文章

  1. AzureDirectory Library for Lucene.Net
  2. 互联网协议 — DNS 域名协议
  3. C++下简单的socket编程
  4. MySQL常用简单小命令
  5. 价格走势PHP源码,php多币种区块链交易所源码 存取功能齐全+最新价格走势正常...
  6. 图解使用Win8Api进行Metro风格的程序开发二----使用文件选择器访问和保存文件
  7. LeetCode 1752. 检查数组是否经排序和轮转得到
  8. dbnetlib sqlserver不存在或拒绝访问_部署IIS+PHP+SQL server环境
  9. 每日一题[LeetCode 315]计算右侧小于当前元素的个数
  10. 计算机网络暗地里范围,《计算机网络应用技术教程》期中试题.doc
  11. 海康录像机怪事:只有第一个通道能取到RTSP流,其他通道都取不到
  12. [环境搭建]-IIS下搭建FTP过程 解决无法连接及534 Policy requires SSL错误
  13. 音视频开发之旅(41)-天空盒
  14. nginx报502错误
  15. python培训抖音广告骗局
  16. 鸿蒙系统如何设置字体,图解鸿蒙OS独特的字体系统!
  17. facebook,twitter,pinterest的分享功能代码--js+html
  18. vscode安装及插件安装
  19. 英语语音篇 - 看词能读
  20. C的回归基础学习1——真的基础

热门文章

  1. spark大数据分析:spark Struct Strreaming(21) 数据流处理
  2. 搭建GPU服务器(容器篇)
  3. Docker下centos7镜像安装中文支持
  4. Ubuntu锐捷认证方法(vmware)
  5. python打印输出世界你好!_Python语句 print(\\\世界,你好”)的输出是?
  6. 量子有什么特性计算机,量子是什么、量子具有什么特性、又有什么作用?
  7. token系统讲解及过期处理
  8. iq2010wifi测试软件,LitePoint IQ2010蓝牙测试仪WIFI/GPS/NFC
  9. trim函数去掉字符串首尾空格
  10. Typora下载安装及使用方法