三种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

相关文章

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene