高级UI晋升之布局ViewGroup(四)

news/2025/2/25 7:18:13

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

一、UI的描述

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

11158618-cac0e2dc292c875b.png

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

二、用户界面布局

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

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

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

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

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

四、 描述一下几个重要的

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

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

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

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

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

11158618-806450345dd6ffaf
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属性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入账号" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入密码" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录" />
</LinearLayout>

4.2RelativeLayout相对布局

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

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

4.3 GridView网格布局

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

11158618-cc5543d29b1702a6.png
image

4.4 ListView列表组件

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

11158618-0d7d799ae329ae8d.png
image

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


http://www.niftyadmin.cn/n/545240.html

相关文章

使用rsync搭建centos的镜像站

简介 自己一直以来相搭建一个开源镜像站&#xff0c;一方面可以了解搭建镜像站的知识&#xff0c;一方面可以同步那些国内没有的linux发行版软件源&#xff0c;但是最主要的原因只是为了好玩 注意点 我这个教程不是专业教程&#xff0c;但是要注意的是镜像站是一个对I/O要求很高…

基础js功能学习笔记

去掉空格 1.前空格&#xff0c;2.后空格&#xff0c;3.前后空格&#xff0c;4.所有空格 function trim(str,type){switch(type){case 1:return str.replace(/^\s*/g,"");case 2:return str.replace(/\s*$/g,"");case 3:return str.replace(/(^\s*)||(\s*$)…

Snowflake Snow Snowflakes(查找)

http://poj.org/problem?id3349 题意&#xff1a;给出n组数据&#xff0c;每组数据有六个数&#xff0c;这n组数据中若有两组数据不管是从某个数顺时针读还是逆时针读都相同&#xff0c;输出“Twin snowflakes found.”&#xff0c;否则&#xff0c;输出 "No two snowfla…

一个DDOS病毒的分析(一)

一、基本信息 样本名称&#xff1a;Rub.EXE 样本大小&#xff1a;21504 字节 病毒名称&#xff1a;Trojan.Win32.Rootkit.hv 加壳情况&#xff1a;UPX(3.07) 样本MD5&#xff1a;035C1ADA4BACE78DD104CB0E1D184043 样本SHA1: BAD1CE555443FC43484E0FACF8B88EA8756F78CB 病…

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

更多Android高级架构进阶视频学习请点击&#xff1a;https://space.bilibili.com/474380680 本篇文章将从自定义View利器Canvas和Paint来进行详解 一、Canvas 为了后文更为方便的讲解Canvas的常用方法的使用&#xff0c;我们先来做一些准备工作&#xff0c;创建一个自定义Vie…

微金所获2015(行业)最具影响力品牌奖

本文讲的是微金所获"2015(行业)最具影响力品牌奖",7月22日&#xff0c;以“新常态下的企业智慧与创新探索”为主题的2015第四届中国财经峰会在北京世纪财富中心举行。众多互联网金融行业从业者济济一堂&#xff0c;大会对优秀的创业企业及人物进行了表彰。 微金所作为…

UC-Android逆向工程师 面试题1的分析

1.简介 这个题目是一位吾爱破解的坛友在面试UC的Android逆向工程事时&#xff0c;遇到的题目。此题不难&#xff0c;与阿里移动去年移动安全比赛的题目差不多&#xff0c;题目的验证方式也是查表对比&#xff0c;并且这个表的数据是放在文件中的。 2.题目分析 直接使用JEB对UC…

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

更多Android高级架构进阶视频学习请点击&#xff1a;https://space.bilibili.com/474380680 本篇文章将从Android 自定义属性动画&Camera动画来介绍自定义View: 1.相关知识点 对于Androi的帧动画&#xff0c;可以制作gif图片&#xff0c;有时为了能够动态的生成帧动画&am…