news 2026/4/15 13:33:59

基本布局(layout)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
基本布局(layout)

总目录

布局就是layout文件中的一种标签,定义了一个容器。不同的布局有不同的特性。

1. 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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="World" /> </LinearLayout>
LinearLayout声名这是一个LinearLayout
android:layout_width布局的宽度(这里是匹配整个父容器的宽度)
android:layout_height布局的高度(这里是匹配整个父容器的高度)
android:orientation容器的子元素的排列方式
android:gravity容器子元素的对其方式
android:padding容器的内边框
android:margin容器的外边框
android:id容器的id
android:background容器的背景颜色(使用16进制颜色)

(注意:其他元素控件和布局也有某些和上述属性重合的属性)

放置在线性布局中的元素按照顺序排列,写在xml代码靠前的位置的控件在界面中排在上面或左边,上述例子中,Hello 与 World 分别排在上面和下面。

线性布局中的元素可以按照一定的权重排列,前提是设置控件宽度或高度为0dp。凡是设置宽度为0dp,其宽度由其权重决定。对于高度也一样。

<TextView android:layout_width="wrap_content" android:layout_height="0dp" android:text="Hello" android:layout_weight="1"/> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:text="World" android:layout_weight="2"/>

上述例子中,第一个文本控件的权重设置为1,第二个为2,说明这两个文本控件的高度之和等于整个容器的总高度,且他们的高度比例是1:2。也就是说,两个文本控件分占据1/3 和 2/3 的父容器高度。宽度也一样。

android:layout_alignParentTop与父容器顶部对齐
android:layout_alignParentBottom与父容器底部对齐
android:layout_alignParentLeft与父容器左侧对齐
android:layout_alignParentRight与父容器右侧对齐
android:layout_centerHorizontal在父容器中水平居中
android:layout_centerVertical在父容器中垂直居中
android:layout_centerInParent在父容器中水平和垂直居中
android:layout_toLeftOf位于指定控件的左侧
android:layout_toRightOf位于指定控件的右侧
android:layout_above位于指定控件的上方
android:layout_below位于指定控件的下方
android:layout_alignTop与指定控件的顶部对齐
android:layout_alignBottom与指定控件的底部对齐
android:layout_alignLeft与指定控件的左侧对齐
android:layout_alignRight与指定控件的右侧对齐
android:layout_margin设置控件的外边距(上下左右)
android:layout_marginTop设置控件的上外边距
android:layout_marginBottom设置控件的下外边距
android:layout_marginLeft设置控件的左外边距
android:layout_marginRight设置控件的右外边距
android:layout_width设置控件宽度(如match_parentwrap_content或固定值)
android:layout_height设置控件高度(如match_parentwrap_content或固定值)

3. ContraintLayout(约束布局)

约束布局通过约束某个空间的顶部,左右,底部来确定空间的位置

<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello ConstraintLayout!" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

上述代码指定文本控件的顶部约束于父容器的顶部,左端约束于父容器的左端......由于每个方向的约束等价,所以他们互相抵消,最终控件的位置处于父容器的正中央。当然,如果取消某个方向的约束,例如左端,控件会立即贴近父容器的右端,因为右约束的存在。当然,约束对象可以设置为其他控件,例如某个按钮约束于文本控件。

layout_constraintLeft_toLeftOf当前视图左侧与目标视图左侧对齐
layout_constraintTop_toTopOf当前视图顶部与目标视图顶部对齐
layout_constraintRight_toRightOf当前视图右侧与目标视图右侧对齐
layout_constraintBottom_toBottomOf当前视图底部与目标视图底部对齐
layout_constraintStart_toStartOf当前视图起始边与目标视图起始边对齐(考虑RTL布局)
layout_constraintEnd_toEndOf当前视图结束边与目标视图结束边对齐(考虑RTL布局)
android:layout_margin视图四边统一外边距
android:layout_marginStart视图起始边外边距
android:layout_marginTop视图顶部外边距
android:layout_marginEnd视图结束边外边距
android:layout_marginBottom视图底部外边距
layout_constraintHorizontal_bias水平方向偏移比例(0.0~1.0,默认0.5居中)
layout_constraintVertical_bias垂直方向偏移比例(0.0~1.0,默认0.5居中)
android:layout_width视图宽度(可设为match_constraint以填充剩余空间)
android:layout_height视图高度(可设为match_constraint以填充剩余空间)
layout_constraintWidth_min视图最小宽度(配合match_constraint使用)
layout_constraintHeight_min视图最小高度(配合match_constraint使用)
layout_constraintWidth_max视图最大宽度(配合match_constraint使用)
layout_constraintHeight_max视图最大高度(配合match_constraint使用)
layout_constraintHorizontal_chainStyle水平链条样式(spreadspread_insidepacked
layout_constraintVertical_chainStyle垂直链条样式(spreadspread_insidepacked
layout_constraintDimensionRatio视图宽高比例(如16:9H,16:9
layout_constraintGuide_begin辅助线距离父容器起始边的距离
layout_constraintGuide_end辅助线距离父容器结束边的距离
layout_constraintGuide_percent辅助线位置百分比(0.0~1.0)
layout_constraintCircle目标视图ID(用于圆形定位)
layout_constraintCircleRadius圆形定位半径
layout_constraintCircleAngle圆形定位角度(0~360度)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 9:05:27

Flink学习笔记:如何做容错

现在我们已经了解了 Flink 的状态如何定义和使用&#xff0c;那 Flink 是如何做容错的呢&#xff1f;今天我们一起来了解一下。 先来回答问题&#xff0c; Flink 是通过状态快照来做容错的&#xff0c;在 Flink 中状态快照分为 Checkpoint 和 Savepoint 两种。 Checkpoint Chec…

作者头像 李华
网站建设 2026/4/15 14:21:02

vue基于springboot的社区健身服务_yob3w0op_

目录 具体实现截图项目介绍论文大纲核心代码部分展示项目运行指导结论源码获取详细视频演示 &#xff1a;文章底部获取博主联系方式&#xff01;同行可合作 具体实现截图 本系统&#xff08;程序源码数据库调试部署讲解&#xff09;同时还支持java、ThinkPHP、Node.js、Spring…

作者头像 李华
网站建设 2026/4/16 9:01:25

题解:AT_abc436_f

题面 Starry Landscape Photo 问题描述 在 AtCoder 行星上看到的夜空中&#xff0c;有 NNN 颗星星&#xff0c;这些星星从东到西排成一条直线。从东方数起的第 iii 颗星&#xff08;1≤i≤N1 \le i \le N1≤i≤N&#xff09;是这些星星中第 BiB _ iBi​ 亮的。 Takahashi 决…

作者头像 李华
网站建设 2026/4/16 9:09:32

每天一个网络知识:什么是堆叠?

在企业网络、数据中心或学校机房中&#xff0c;我们常常会看到多个交换机整齐排列在机柜里。随着网络规模增加&#xff0c;设备数量越来越多&#xff0c;如何让这些交换机更高效地协同工作、简化管理、提高可靠性&#xff1f; 其中一个非常重要的技术就是 “堆叠&#xff08;S…

作者头像 李华
网站建设 2026/4/16 1:42:56

Django WiFi文件分享

项目介绍 在日常工作和生活中,我们经常需要在电脑和手机之间传输文件。传统的传输方式要么需要数据线连接,要么需要借助第三方应用,操作繁琐且不够高效。今天,我将介绍一个基于Django开发的WiFi文件分享应用,它可以让你通过电脑选择本地文件夹,生成访问二维码,然后通过…

作者头像 李华
网站建设 2026/4/16 1:40:56

《高压电气连接器必备指南》

高压电气连接器对于工作电压超过 60V 的电路以及汽车和工业应用中的关键组件至关重要。它们促进大电流的传输——特别是在电动汽车中——连接电池组、电机控制器和充电器等关键部件。高压电气连接器中使用的材料以下是在高压连接器开发和使用中常用的关键材料&#xff1a;导电材…

作者头像 李华