更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680
本篇文章将从LinearLayout、RelativeLayout、FrameLayout、AbsoluteLayout、GridLayout来介绍常用View布局:

一、UI的描述

对于Android应用程序中,所有用户界面元素都是由ViewViewGroup对象构建的。View是绘制在屏幕上能与用户进行交互的一个对象。而对于ViewGroup来说,则是一个用于存放其他ViewViewGroup对象的布局容器!

 
 

Android为我们提供了ViewViewGroup的两个子类的集合,提供常用的一些输入控件(比如按钮,图片和文本域等)和各种各样的布局模式(比如线程布局,相对布局,绝对布局,帧布局,表格布局等)。

二、用户界面布局

在你APP软件上的,用户界面上显示的每一个组件都是使用层次结构ViewViewGroup对象来构成的,比如,每个ViewGroup都是不可见容器,每个ViewGroup视图组用于组织子视图View的容器,而它的子视图View可能是输入一些控件或者在某块区域的小部件UI。如果你有了层次结构树,你可以根据自己的需要,设计出一些布局,但要尽量简单,因为越简单的层次结构最适合性能。

要声明布局,可以在代码中实例化对象并构建,最简单的方法也可以使用xml文件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView android:id="@+id/text"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="TextView" />
  10. <Button android:id="@+id/button"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Button" />
  14. </LinearLayout>

三、 在Android中提供了几个常用View布局:

  1. LinearLayout线性布局
  2. RelativeLayout相对布局
  3. FrameLayout帧布局
  4. AbsoluteLayout绝对布局
  5. TableLayout表格布局
  6. GridLayout网格布局

四、 描述一下几个重要的

线性布局:
指子控件以水平或垂直方式排列。

相对布局:
指子控件以控件之间的相对位置或子控件相对于父容器的位置排列。

帧布局:
指所有子控件均放在左上角且后面元素直接覆盖在前面元素之上。

绝对布局:
指子控件通过绝对定位x,y位置来决定其位置摆放。

表格布局:
指以行列的形式放置子控件,每一行是一个TableRow对象或者View对象。

 
image

4.1 LinearLayout线性布局

常用属性:

  1. id:为该组件添加一个资源id
  2. orientation:布局中的排列方式,有两种方式:
    horizontal水平
    vertical竖直
  3. layout_width:布局的宽度,用wrap_content表示组件的实际宽度,match_parent表示填充父容器
  4. layout_height:布局的长度,用wrap_content表示组件的实际长度,match_parent表示填充父容器
  5. gravity:控制组件所包含的子元素的对齐方式
  6. layout_gravity:控制该组件在父容器里的对齐方式
  7. background:为该组件添加一个背景图片

LinearLayout是一个视图组,可以在一个方向垂直或者水平分布所有子项,用android:orientation属性。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <EditText
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:hint="输入账号" />
  10. <EditText
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:hint="输入密码" />
  14. <Button
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="登录" />
  18. </LinearLayout>

4.2RelativeLayout相对布局

RelativeLayout是一个相对布局的视图组,用来显示相对位置的子视图类,在默认情况下,所有子视图对会分布在左上角。

  1. layout_alignParentTop:true,视图的上边界与父级的上边界对齐
  2. layout_centerVertical:true,将子类放置在父类中心
  3. layout_below:将该视图放在资源ID下方
  4. layout_toRightOf:将该视图放在资源ID右边
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <EditText
  6. android:id="@+id/name"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:hint="你的名字" />
  10. <Button
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_below="@id/name"
  14. android:layout_alignParentRight="true"
  15. android:text="正确" />
  16. </RelativeLayout>

4.3 GridView网格布局

GridView其实是一个网格一样的视图组件,是一个ViewGroup的二维视图。用适配器可以将布局进行填充。

 
 

4.4 ListView列表组件

ListView是一个用于显示列表的可以滚动的视图组,列表项也可以用适配器进行添加内容的。

 
 

更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680
原文链接:https://www.jianshu.com/p/58d63e31ea18

高级UI晋升之布局ViewGroup(四)的更多相关文章

  1. 高级UI晋升之常用View(三)中篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从ViewPager来介绍常用View:文章目录 一.简介 二.基本使用 ...

  2. 高级UI晋升之View渲染机制(二)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 优化性能一般从渲染,运算与内存,电量三个方面进行,今天开始说聊一聊Android ...

  3. 高级UI晋升之常用View(三)上篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将先从以下两个内容来介绍常用View: [RecycleView] [Ca ...

  4. 高级UI晋升之触摸事件分发机制(一)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 0. 前言 鉴于安卓分发机制较为复杂,故分为多个层次进行讲解,分别为基础篇.实践 ...

  5. 高级UI晋升之自定义View实战(八)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章自定义流式布局来进行介绍: 一般常见的流式布局由两种,一种是横向的个数固定 ...

  6. 高级UI晋升之自定义view实战(七)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章自定义ViewGroup实现瀑布流效果来进行详解dispatchTouch ...

  7. 高级UI晋升之自定义View实战(六)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义V ...

  8. 高级UI晋升之常用View(三)下篇

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从WebView来介绍常用View: 一.WebView介绍 Andro ...

  9. 高级UI晋升之自定义View实战(五)

    更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680本篇文章将从自定义View利器Canvas和Paint来进行详解 一.Canvas ...

随机推荐

  1. mysql 内连接和外连接查询

    一.内连接查询 (笛卡儿积) 内联接查询inner join,mysql可以简写为join 二.外连接查询 左外联接查询left outer join,mysql可以简写为left join右外联接查 ...

  2. PHP定界符<<<的使用方法

    在web编程过程中难免会遇到用echo来输出大段的html和javascript脚本的情况,如果用传统的输出方法——按字符串输出的话,使用PHP肯定要有大量的转义符来对字符串中的引号''/" ...

  3. vue项目工具文件utils.js javascript常用工具类,javascript常用工具类,util.js

    vue项目工具文件utils.js :https://blog.csdn.net/Ajaxguan/article/details/79924249 javascript常用工具类,util.js : ...

  4. Linux利器 strace [看出process呼叫哪個system call]

    Linux利器 strace strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必 ...

  5. sublime推荐插件

    SyncedSidebarBg:侧边栏底色统一 Emmet:集合多种功能,大名鼎鼎的 Zen coding ==> 不过对于嵌入式的我没多大用啊 Sublime CodeIntel:代码提示 A ...

  6. ASP.NET MVC 学习笔记之TempData、HttpContext和HttpContextBase杂谈

    TempData本质上是Session 但是有一点不同的是,TempData被赋值之后,一旦被Action访问一次之后,马上就会清空. System.Web.HttpContext 和System.W ...

  7. java如何生成一个0-100的随机整数?

    public class Test {public static void main(String[] args) {int num=(int)(Math.random()*101);System.o ...

  8. 同时安装不同版本jdk引起的冲突解决方法

    https://blog.csdn.net/xiongyouqiang/article/details/79240521 现象 由于工作原因, 之前用的jdk1.8版本,因为线上生产环境都是jdk1. ...

  9. PHP创建多级目录文件夹

    PHP创建多级目录的代码实例如下: <?php function create_dir($dirName) { // 去除输入目录名中的空格部分 $dirName = trim($dirName ...

  10. go静态类型

    go静态类型 静态类型语言意味着变量必须指定一个类型,例如整型.字符串.布尔型和数组等.可以在声明变量时指定变量类型.大多数情况下,让编译器自动去推断变量类型(我们将看到一些简单的例子). 关于静态类 ...