Android移动应用开发指南之六种布局详解
作者:Icy?Hunter 时间:2022-09-10 06:23:44
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设置权重(感觉和弹性盒子差不多)
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>
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>
简单来说,就是可以叠一起的布局
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>
可以看成类似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>
可以看成TableLayout升级版?
ConstraintLayout
约束布局
这个应该是最强的布局了
创建布局默认的就是这个了。
打开design模式,然后随便拖几个按钮进去
点击魔术棒建立约束。
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>
我们开个虚拟机运行一下:
只能说,差不太多,微调一下差不多就能用了。
也能够设置各个组件的属性值颜色字体等等。
这用起来就像是墨刀一样。
参考
https://www.bilibili.com/video/BV1Jb4y187C4/?p=25&spm_id_from=pageDriver&vd_source=f57738ab6bbbbd5fe07aae2e1fa1280f
来源:https://blog.csdn.net/qq_52785473/article/details/126979334
标签:android,移动应用开发,布局
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现画线、矩形、椭圆、字符串功能
2022-12-30 19:24:33
![](https://img.aspxhome.com/file/2023/0/72590_0s.jpg)
Java上传文件进度条的实现方法(附demo源码下载)
2023-06-06 11:06:16
spring 整合mybatis后用不上session缓存的原因分析
2021-12-09 10:11:03
C# 用什么方法将BitConverter.ToString产生字符串再转换回去
2021-10-07 17:49:40
C#使用HttpClient的正确方式你了解吗
2023-07-04 12:12:40
![](https://img.aspxhome.com/file/2023/1/129721_0s.png)
java实现单词搜索迷宫游戏
2023-11-10 22:44:32
Android实现歌词渐变色和进度的效果
2023-09-24 02:33:17
![](https://img.aspxhome.com/file/2023/8/91708_0s.png)
java发送http请求并获取状态码的简单实例
2023-12-06 00:59:14
WPF实现调用本机摄像头的示例代码
2023-03-15 15:40:24
![](https://img.aspxhome.com/file/2023/6/68076_0s.png)
java理论基础Stream元素的匹配与查找
2021-08-26 10:11:20
![](https://img.aspxhome.com/file/2023/8/122948_0s.jpg)
java 深拷贝与浅拷贝机制详解
2023-02-18 19:00:59
详解备忘录模式及其在Java设计模式编程中的实现
2023-08-24 22:34:02
![](https://img.aspxhome.com/file/2023/6/58436_0s.jpg)
C语言实现字符串拼接和拷贝
2021-06-07 14:50:09
Java实现二分查找的变种
2023-11-07 11:26:28
![](https://img.aspxhome.com/file/2023/6/85556_0s.jpg)
Java原生HttpClient的使用详解
2022-06-04 16:29:49
![](https://img.aspxhome.com/file/2023/7/125257_0s.jpg)
Java NIO Buffer实现原理详解
2023-12-10 22:37:37
![](https://img.aspxhome.com/file/2023/8/78268_0s.png)
Java关键字instanceof用法及实现策略
2023-09-23 06:05:38
![](https://img.aspxhome.com/file/2023/0/58560_0s.png)
JavaWeb中使用JavaMail实现发送邮件功能实例详解
2023-01-07 13:54:37
![](https://img.aspxhome.com/file/2023/1/60821_0s.png)
详解Android Webview加载网页时发送HTTP头信息
2023-11-16 02:32:34
Spring Data环境搭建实现过程解析
2022-02-26 20:13:38