Android移动应用开发指南之六种布局详解
作者:Icy?Hunter 发布时间:2022-09-10 06:23:44
标签:android,移动应用开发,布局
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


猜你喜欢
- MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对
- 调用native 方法来开启和关闭vibrator: native static void vibratorOn(long millisec
- 1.概述其实最简单的办法就是使用原生sql,如 session.createSQLQuery("sql"),或者使用jd
- 使用@RequestParam注解获取参数创建Hello控制器类package com.controller;import org.spri
- 本文实例为大家分享了C#无损高质量压缩图片的具体代码,供大家参考,具体内容如下/// 无损压缩图片 /// <param
- 如何实现封装可以分为两步:第一步:将类的变量声明为private。第二步:提供公共set和get方法来修改和获取变量的值。代码展示publi
- 通过输入流来读取客户端信息,相应的时候通过输出流来实现。服务端类的代码:import java.io.BufferedReader;impo
- 单例类保证一个类全局仅有一个实例,并提供一个全局访问点,由于只能生成一个实例,因此我们必须把构造函数设为私有函数以禁止他人创建实例。实现1:
- 本文实例讲述了Android编程实现获取当前连接wifi名字的方法。分享给大家供大家参考,具体如下:WifiManager wifiMgr
- using System; using System.IO; namespace DelAllLrcFiles { class Progra
- 1、图的定义我们知道,前面讨论的数据结构都有一个框架,而这个框架是由相应的算法实现的,比如二叉树搜索树,左子树上所有结点的值均小于它的根结点
- Web UI项目中, 很多 Spring controller 视图函数直接返回 html 页面, 还有一些视图函数是要重定向或转发到其他的
- 在java.lang.Runtime.exec的使用中,我们经常会用到将重定向命令执行的输入/结果或者将错误信息读取出来.那么,在使用过程中
- 本文实例为大家分享了java金额数字转中文工具类的具体代码,供大家参考,具体内容如下java金额数字转中文工具类ConvertNum.jav
- 本文以实例代码实现了C#根据数字序号输出星期几,用户可通过输入数字0~6,输出星期各天的英语单词,程序中主要是演示if语句和switch语句
- 一直想练习下java多线程抓取数据。有天被我发现,铃声多多的官网(http://www.shoujiduoduo.com/main/)有大量
- Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的。 可以把Fragment
- 关于java的字符串处理我们一般使用String类和StringBuffer类那么String类和StringBuffer类的区
- 废话不多说了,直接给大家贴代码了,具体代码如下所示:<?xml version="1.0" encoding=&q
- 前言:发送邮件,肯定是每个公司都会有的基本业务。很多公司都会选择把发送邮件作为一个基础服务,对外提供接口。直接调用就可发邮件了。但是我们都知