三种Android布局方式:LinearLayout

2024-03-23 07:08

本文主要是介绍三种Android布局方式:LinearLayout,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

LinearLayout线性布局

控件的排列方式:垂直排列、水平排列

android:orientation 属性:

可选值为 vertical (垂直的)和 horizontal (水平的)

     

如果不指定 android:orientation 默认值是 horizontal (水平)

如果排列方式是水平,则控件宽度不能设置成match_parent。

android:layout_gravity  属性和 android:gravity 属性不同:

android:gravity:指的是文字在控件中的对齐方式

android:layout_gravity :指的是控件在布局中的对齐方式

当LinearLayout的排列方向是horizontal时,只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的。

同样的道理,当LinearLayout的排列方向是vertical时,只有水平方向上的对齐方式才会生效。

还有一个重要属性:android:layout_weight

允许我们使用比例的方式来控制控件的大小。它在手机屏幕的适配性方面可以起到非常重要的作用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:id="@+id/input_message"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="Type something"/><Buttonandroid:id="@+id/send"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="Send"/>
</LinearLayout>

效果如图:

 实现原理很简单,Activity会将所有的android:layout_weight相加起来,然后每个控件所占大小的比例就是用该控件的layout_weight值除以刚才算出的总值。

我们同样可以设置部分其他控件的宽度,来实现更好的效果,对于上述,我们可以修改button 的 width,写成:

    <Buttonandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="Send"/>

得到的效果如图:

 RelativeLayout相对布局

与线性布局不同,相对布局更加随意,通过相对定位的方式进行布局。

相对父布局定位:

    <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1"android:layout_alignParentTop="true"android:layout_alignParentLeft="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 3"android:layout_alignParentTop="true"android:layout_alignParentRight="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 4"android:layout_alignParentBottom="true"android:layout_alignParentRight="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 5"android:layout_centerInParent="true" />

效果如图:

 所用到的属性为:

android:layout_alignParentLeft、android:layout_alignParentTop、android:layout_alignParentRight、android:layout_alignParentBottom、android:layout_centerInParent

还可以相对于控件进行定位:相对于控件进行布局需要对每一个控件设置一个  id

所用到的属性有:layout_above(id)、layout_below(id)、layout_toRightOf(id)、layout_toLeftOf(id)

当运用到某个id时,要按顺序,否则会找不到id!!

还有另一组对齐的属性:android:layout_alignLeft、android:layout_alignRight、android:layout_alignTop、android:layout_alignBottom

分别为与某个控件的左边缘、右边缘、顶边缘、底边缘对齐。

FrameLayout帧布局

定位方式欠缺:这种布局没有丰富的定位方式,所有的控件都会默认摆放在布局的左上角。

这篇关于三种Android布局方式:LinearLayout的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/837523

相关文章

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计