安卓的相对布局与线性布局

article/2025/9/28 0:20:59

一、安卓布局的种类

Android共有七大基本布局。
分别是:线性布局LinearLayout、表格布局TableLayout、相对布局RelativeLayout、帧布局FrameLayout、绝对布局AbsoluteLayout、网格布局GridLayout。约束布局ConstraintLayout。
其中,表格布局是线性布局的子类。网格布局是android 4.0后新增的布局。约束布局是Android Studio 2.2推出的新布局,并从Android Studio 2.3开始成为默认布局。
在手机程序设计中,绝对布局基本上不用,用得相对较多的是线性布局和相对布局。以下对线性布局和相对布局大致作一个概述。

二、相对布局

相对布局,顾名思义就是通过相对定位的方式让控件出现在布局的任何位置。

1.常用的基础控件

Button,TextView,EditText,ImageView

2.RelativeLayout中子控件常用属性

1、相对于父控件
例如:android:layout_alignParentTop=“true”
android:layout_alignParentTop 控件的顶部与父控件的顶部对齐;
android:layout_alignParentBottom 控件的底部与父控件的底部对齐;
android:layout_alignParentLeft 控件的左部与父控件的左部对齐;
android:layout_alignParentRight 控件的右部与父控件的右部对齐;

2、相对给定Id控件
例如:android:layout_above=“@id/…”
android:layout_above 控件的底部置于给定ID的控件之上;
android:layout_below 控件的底部置于给定ID的控件之下;
android:layout_toLeftOf 控件的右边缘与给定ID的控件左边缘对齐;
android:layout_toRightOf 控件的左边缘与给定ID的控件右边缘对齐;
android:layout_alignBaseline 控件的baseline与给定ID的baseline对齐;
android:layout_alignTop 控件的顶部边缘与给定ID的顶部边缘对齐;
android:layout_alignBottom 控件的底部边缘与给定ID的底部边缘对齐;
android:layout_alignLeft 控件的左边缘与给定ID的左边缘对齐;
android:layout_alignRight 控件的右边缘与给定ID的右边缘对齐;

3、居中
例如:android:layout_centerInParent=“true”
android:layout_centerHorizontal 水平居中;
android:layout_centerVertical 垂直居中;
android:layout_centerInParent 父控件的中央;

3.示例代码

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><RelativeLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#4EEE94"   ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="heavy-sea.智能家居" android:layout_centerVertical="true"android:layout_marginLeft="20dp"android:textSize="15dp"  /><Button android:id="@+id/b1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="注册"android:layout_alignParentRight="true"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查询"android:layout_toLeftOf="@id/b1"android:layout_marginRight="20dp"/></RelativeLayout><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/pic_rf"android:layout_centerInParent="true"	    /><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/card"android:layout_centerInParent="true"android:paddingLeft="100dp"    /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/btn_selector"android:text="刷卡"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true" android:layout_marginBottom="10dp"/></RelativeLayout>

布局显示:
在这里插入图片描述

三、线性布局

线性布局会将它所包含的控件在线性方向上依次排列。
在这个布局主要通过设置 android:orientation 属性来指定布局的排列方式。如果为 vertical 则在垂直方向上线性排列,如果为 horizontal 则会在水平方向上排列。

1.常用属性

android:orientation 指定布局的排列方式,如果为 vertical 则在垂直方向上线性排列,如果为 horizontal 则会在水平方向上排列。
android:gravity 内部控件对齐方式,常用属性值有center、center_vertical、center_horizontal、top、bottom、left、right等,同时可多个组合,如(left|buttom)。
android:layout_gravity 该组件在父容器中的对齐方式
android:id 为该组件设置一个ID,在java文件中可以通过findViewById(id)找到组件

2.Weight(权重)

android:layout_weight:权重,用来分配当前控件在剩余空间的大小。
简单的用法可概括为要等比例划分控件所在的空间,控件本身占空间比例为多少。

3.divider分割线

用于为Linearlayout设置分割线图片,通过showDividers设置分割线的所在位置
android:divider 设置分割线的图片
android:showDividers 设置分割线的所在位置,可选none,middle,begining,end
android:dividerPadding 设置分割线的padding

4.示例代码

line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line" ><sizeandroid:height="2dp"android:width="20dp" /><stroke android:color="#00ff00" /></shape>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><LinearLayoutandroid:layout_width="300dp"android:layout_height="200dp"android:layout_centerInParent="true"android:orientation="horizontal" ><LinearLayoutandroid:layout_width="0dp"android:layout_height="200dp"android:layout_weight="2"android:divider="@drawable/line"android:orientation="vertical"android:showDividers="middle|end" ><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal|bottom"android:text="账号"android:textSize="20dp" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal|bottom"android:text="昵称"android:textSize="20dp" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center_horizontal|bottom"android:text="密码"android:textSize="20dp" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="200dp"android:layout_weight="4"android:orientation="vertical" ><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /></LinearLayout></LinearLayout></RelativeLayout>

该布局中先整体来一个线性布局,把所有的TextView和所有的EditText分别看成一个整体,二者用orientation指定排列方式为水平的。
再对所有的TextView来一个线性布局,里面所有的TextView用orientation指定排列方式为垂直的。
再对所有的EditText来一个线性布局,里面所有的EditText用orientation指定排列方式为垂直的。
布局显示
在这里插入图片描述


http://chatgpt.dhexx.cn/article/GxwJWOj0.shtml

相关文章

Android:布局

Android&#xff1a;布局 LinearLayoutRelativeLayoutFrameLayoutTableLayoutGridLayoutConstraintLayout LinearLayout <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_height"match_parent"android:layout_…

安卓六大布局之 线性布局(LinearLayout)

Android的界面是有布局和组件协同完成的&#xff0c;布局好比是建筑里的框架&#xff0c;而组件则相当于建筑里的砖瓦。组件按照布局的要求依次排列&#xff0c;就组成了用户所看见的界面。 Android的六大布局分别是 LinearLayout&#xff08;线性布局&#xff09;RelativeLayo…

Android-布局管理器

线性布局(Linearlayout) 属性 orientation 布局管理器内组件的排列方式(horizontal&#xff08;水平&#xff09;和vertical&#xff08;垂直&#xff09;&#xff0c;默认值为 horizontal.) layout_weight 权重 用于设置组件占父容器剩余空间的比例 la…

android 布局

android学习笔记&#xff08;一 android布局学习&#xff09; 转自http://blog.sina.com.cn/s/blog_61c62a960100ev3q.html (2009-09-20 20:50:44) 转载 标签&#xff1a; it 分类&#xff1a;android 最近痴迷上了android &#xff0c; 因为有java 语言的基础学起来自己感觉很…

安卓六大布局介绍

安卓六大布局 布局的介绍安卓六大布局 布局的介绍 用户使用安卓看到的应用界面&#xff0c;是通过布局和组件构成的&#xff0c;组件根据布局的格式排列&#xff0c;形成用户所看到的界面。 安卓六大布局 线性布局方式&#xff08;LinearLayout&#xff09; 按照垂直或者水平…

安卓线性布局

安卓线性布局 &#xff08;一&#xff09;界面与布局1、界面2、布局&#xff08;1&#xff09;UI容器&#xff08;2&#xff09;UI控件 (Control)&#xff08;3&#xff09;两种方式声明布局 &#xff08;二&#xff09;线性布局&#xff08;1&#xff09;常用属性 &#xff08…

安卓的常用布局看一篇就够了

目录 1-1 布局通用的属性 1-2 线性布局&#xff08;LinearLayout&#xff09; 1、常见属性&#xff1a; 2、线性布局的例子&#xff1a; 1-3 相对布局&#xff08;RelativeLayout&#xff09; 1、常见属性&#xff1a; 2、 相对布局的例子&#xff1a; 1-4 帧布局&a…

android布局技巧:创建高效布局

Android UI工具包提供了一些布局管理器&#xff0c;它们使用起来相当容易&#xff0c;而且&#xff0c;大多数的时候&#xff0c;你只需要使用它们最基本的特征来实现UI。 执着于基本特征的使用对于创建UI来说&#xff0c;往往不是最高效的。一个常见的例子就是滥用LinearLayo…

安卓7大基本布局

一&#xff1a;基础知识 1.Android七大基本布局分别是&#xff1a; LinearLayout(线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)、FrameLayout(层布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局)、ConstraintLayout(约束布局)。 2.七大基本布局的继承…

Android的六大基本布局

线性布局 LinearLayout相对布局 RelativeLayout表格布局 TableLayout绝对布局 AbsoluteLayout网格布局 GridLayout帧布局 FrameLayout 布局通用属性 属性名称功能描述android:id设置布局的标识android:layout_width设置布局的宽度android:layout_height设置布局的高度android:…

安卓布局详解:探索各种布局方式

文章目录 前言一、线性布局&#xff08;LinearLayout&#xff09;二、相对布局&#xff08;RelativeLayout&#xff09;三、帧布局&#xff08;FrameLayout&#xff09;四、表格布局&#xff08;TableLayout&#xff09;五、约束布局&#xff08;ConstraintLayout&#xff09;六…

微信小程序页面布局——上中下结构

小程序页面布局——上中下结构 内容简述 上中下结构&#xff1a;头脚固定中间滚动框 使用UI框架&#xff1a;Vant Weapp(引入安装参考) 为了方便&#xff0c;使用了less生成wxss&#xff0c;所以展示的是less代码&#xff0c;有需要可以看&#xff1a;https://www.jianshu.com…

微信小程序中的常用布局方式(总结)

参照Android开发&#xff0c;总结了微信小程序的常用的两种布局方式&#xff1a;1、线性布局&#xff08;横版、竖版&#xff09;。2、网格布局。 效果图如下&#xff1a; 一、网格布局 &#xff08;1&#xff09;固定Item个数的网格布局&#xff0c;主要用于功能模块入口展示…

微信小程序页面布局

一,微信小程序页面布局方式采用的是Flex布局 1.Flex布局&#xff0c;是W3c在2009年提出的一种新的方案&#xff0c;可以简便&#xff0c;完整&#xff0c;响应式的实现各种页面布局。 2.Flex布局提供了元素在容器中的对齐&#xff0c;方向以及顺序&#xff0c;甚至他们可以是动…

html的网格布局

网格布局 学习总结&#xff1a; 从7.19进入csdn夏令营后&#xff0c;感谢各位老师的辛苦讲解与发布任务&#xff0c;我对C1能力认证中web方面的知识有了极大的领会。学习过程中既温习了在校学习的知识&#xff0c;也学到了诸如网格布局&#xff0c;动画&#xff0c;less&#x…

网格布局(grid布局)

网格布局 他可以将页面分为多个网格&#xff0c;可以任意组合不同的网格 &#xff0c;做出各种各样的布局。 网格布局为二维性质的。 设置行、列间距 grid-row-gap:1rem ;行间距 ** grid-column-gap: 1rem ;列间距** ** grid-gap: 1rem;**设置行列间距 设置容器的列宽和与…

CSS布局—网格布局Grid(一)

CSS网格可以定义由行和列组成的二维布局&#xff0c;然后将元素放置到网格中。有些元素可能只占据网格的一个单元&#xff0c;另一些元素则可能占据多行或多列。网格的大小既可以精确定义&#xff0c;也可以根据自身内容自动计算。你既可以将元素精确地放置到网格某个位置&…

CSS Grid 网格布局教程

一、概述 网格布局&#xff08;Grid&#xff09;是最强大的 CSS 布局方案。 它将网页划分成一个个网格&#xff0c;可以任意组合不同的网格&#xff0c;做出各种各样的布局。以前&#xff0c;只能通过复杂的 CSS 框架达到的效果&#xff0c;现在浏览器内置了。 上图这样的布局&…

css 网格布局

简介&#xff1a; 网格是由一系列水平及垂直的线构成的一种布局模式。一个网格通常具有许多的列&#xff08;column&#xff09;与行&#xff08;row&#xff09;&#xff0c;以及行与行、列与列之间的间隙&#xff0c;这个间隙一般被称为沟槽&#xff08;gutter&#xff09;。…

微信小程序的页面布局(1)

微信小程序的页面布局主要用到两个文件&#xff0c;wxml&#xff08;摆放各种组件&#xff09;和wxss&#xff08;设计排版&#xff09; 因此&#xff0c;我们首先将要用到的组件按照一定的组织排序扔进wxml文件里&#xff0c;什么叫组织排序呢&#xff0c;这里注意就是组件与组…