Android移动应用开发指南之六种布局详解


Posted in Java/Android onSeptember 23, 2022

LinearLayout

<?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"
    android:dividerPadding="200dp"
    >

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#ff0000"
        android:layout_weight="1"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ff000000"
        />
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:background="#ffff00"
        android:layout_weight="1"
        />

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="00dp"
        android:layout_weight="1"
        android:background="#00ffff" />
</LinearLayout>

orientation设置排列方式

layout_weight设置权重(感觉和弹性盒子差不多)

Android移动应用开发指南之六种布局详解

RelativeLayout

顾名思义,相对元素布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="10dp"
    >
    <RelativeLayout
        android:id="@+id/rl1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        android:layout_centerInParent="true"
        />
    <RelativeLayout
        android:layout_margin="0dp"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#00ff00"
        android:layout_toLeftOf="@+id/rl1"
        />

</RelativeLayout>

Android移动应用开发指南之六种布局详解

FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <FrameLayout
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="#ff0000"
        />
    <FrameLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#ffff00"
        android:foreground="@drawable/a"
        />

    <FrameLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#00ff00"/>

</FrameLayout>

Android移动应用开发指南之六种布局详解

简单来说,就是可以叠一起的布局

TableLayout

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:collapseColumns=""

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第1个"
        />
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个"
            />
    </TableRow>

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个"
            />
    </TableRow>

</TableLayout>

Android移动应用开发指南之六种布局详解

可以看成类似excel的表格一样的布局

通常结合< TableRow >一起使用

GridLayout

网格布局

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:columnCount="3"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        android:text="第一个"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三个"
        android:layout_columnSpan="3"
        />
</GridLayout>

Android移动应用开发指南之六种布局详解

可以看成TableLayout升级版?

ConstraintLayout

约束布局

这个应该是最强的布局了

创建布局默认的就是这个了。

Android移动应用开发指南之六种布局详解

打开design模式,然后随便拖几个按钮进去

Android移动应用开发指南之六种布局详解

点击魔术棒建立约束。

ok完成布局了。

代码也自动生成好了:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="246dp"
        android:layout_marginTop="107dp"
        android:text="按钮"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="172dp"
        android:layout_marginBottom="125dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="115dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我们开个虚拟机运行一下:

Android移动应用开发指南之六种布局详解

只能说,差不太多,微调一下差不多就能用了。

也能够设置各个组件的属性值颜色字体等等。

这用起来就像是墨刀一样。

参考

https://www.bilibili.com/video/BV1Jb4y187C4/?p=25&spm_id_from=pageDriver&vd_source=f57738ab6bbbbd5fe07aae2e1fa1280f

总结

到此这篇关于Android移动应用开发指南之六种布局的文章就介绍到这了,更多相关Android移动应用开发布局内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
Spring Boot两种全局配置和两种注解的操作方法
Jun 29 Java/Android
总结Java对象被序列化的两种方法
Jun 30 Java/Android
Spring中bean的生命周期之getSingleton方法
Jun 30 Java/Android
Java日常练习题,每天进步一点点(38)
Jul 26 Java/Android
springboot 启动如何排除某些bean的注入
Aug 02 Java/Android
SpringBoot+VUE实现数据表格的实战
Aug 02 Java/Android
java代码实现空间切割
Jan 18 Java/Android
Java 超详细讲解IO操作字节流与字符流
Mar 25 Java/Android
Android基于Fresco实现圆角和圆形图片
Apr 01 Java/Android
Java基础——Map集合
Apr 01 Java/Android
Java 多线程协作作业之信号同步
May 11 Java/Android
Java 多线程并发FutureTask
Jun 28 Java/Android
Java中的Kafka为什么性能这么快及4大核心详析
Mybatis 一级缓存和二级缓存原理区别
Sep 23 #Java/Android
Java实现贪吃蛇游戏的示例代码
Sep 23 #Java/Android
Java获取字符串编码格式实现思路
Sep 23 #Java/Android
java获取一个文本文件的编码(格式)信息
Sep 23 #Java/Android
JDK8中String的intern()方法实例详细解读
Sep 23 #Java/Android
Spring boot实现上传文件到本地服务器
Aug 14 #Java/Android
You might like
用PHP动态生成虚拟现实VRML网页
2006/10/09 PHP
常用的php对象类型判断
2008/08/27 PHP
PHP操作MongoDB时的整数问题及对策说明
2011/05/02 PHP
PHP数组无限分级数据的层级化处理代码
2012/12/29 PHP
ThinkPHP实现带验证码的文件上传功能实例
2014/11/01 PHP
php模板引擎技术简单实现
2016/03/15 PHP
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
JS中的public和private对象,即static修饰符
2012/01/18 Javascript
jquery实现侧边弹出的垂直导航
2014/12/09 Javascript
Jquery 实现table样式的设定
2015/01/28 Javascript
js仿京东轮播效果 选项卡套选项卡使用
2017/01/12 Javascript
微信小程序实战之运维小项目
2017/01/17 Javascript
Javascript前端经典的面试题及答案
2017/03/14 Javascript
9种改善AngularJS性能的方法
2017/11/28 Javascript
webpack打包并将文件加载到指定的位置方法
2018/02/22 Javascript
Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)
2018/05/10 Javascript
JS中的算法与数据结构之集合(Set)实例详解
2019/08/20 Javascript
原生js实现文件上传、下载、封装等实例方法
2020/01/05 Javascript
vue项目使用$router.go(-1)返回时刷新原来的界面操作
2020/07/26 Javascript
vue-router定义元信息meta操作
2020/12/07 Vue.js
[14:57]DOTA2 HEROS教学视频教你分分钟做大人-幽鬼
2014/06/13 DOTA
Python引用(import)文件夹下的py文件的方法
2014/08/26 Python
python遍历文件夹下所有excel文件
2018/01/03 Python
Python+OpenCV图片局部区域像素值处理详解
2019/01/23 Python
详解pandas如何去掉、过滤数据集中的某些值或者某些行?
2019/05/15 Python
教你如何用python操作摄像头以及对视频流的处理
2020/10/12 Python
使用css实现android系统的loading加载动画
2019/07/25 HTML / CSS
把富文本的回车转为br标签
2019/08/09 HTML / CSS
美国知名艺术画网站:Art.com
2017/02/09 全球购物
办公室助理岗位职责
2013/12/25 职场文书
运动会入场解说词
2014/02/07 职场文书
2014年预备党员学习新党章思想汇报
2014/09/15 职场文书
网络管理员岗位职责
2015/02/12 职场文书
档案管理员岗位职责
2015/02/12 职场文书
新党员入党决心书
2015/09/22 职场文书
Win10加载疑难解答时出错发生意外错误的解决方法
2022/07/07 数码科技