news 2026/2/22 15:18:39

基本布局(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/2/13 1:07:25

AI大模型赋能消费升级:新机遇与新路径

随着“人工智能”行动的深入实施&#xff0c;AI大模型正从技术概念加速转化为消费市场的新动能。从智能终端的个性化交互到虚实融合的购物场景&#xff0c;从创意设计的效率革新到教育服务的精准适配&#xff0c;AI大模型正在重构消费全链条&#xff0c;推动消费从功能满足向体…

作者头像 李华
网站建设 2026/2/6 20:27:37

AI赋能医学教育:从知识传递到能力塑造的革命

当医学生不再局限于书本图解和标本观察&#xff0c;而是能与AI虚拟病人沉浸式问诊、借助数字人解剖台探索人体奥秘&#xff0c;医学教育的传统边界正被人工智能彻底打破。广东医科大学AI医学院打造的“师-机-生”协同共创模式&#xff0c;正是AI技术深度赋能教育领域的生动实践…

作者头像 李华
网站建设 2026/2/18 21:08:34

初步了解Next.js

Next.js是React的一个超集框架&#xff0c;为什么会有Next.js这个框架存在呢&#xff0c;因为它可以解决React的痛点&#xff0c;就是客户端渲染 Next.js的一大亮点就是支持多模式混合渲染&#xff0c;分别有四种模式&#xff0c;CSR&#xff0c;SSR&#xff0c;SSG&#xff0c…

作者头像 李华
网站建设 2026/2/22 3:38:11

使用cmake构建Cplusplus版运行时库-–-behaviac

原文 请首先到/language/zh/downloads/下载或克隆源码。 缺省的&#xff0c;我们使用cmake来生成对应平台的项目文件&#xff08;sln或make文件等&#xff09;。 但cmake不是必须的&#xff0c;也可以选择自己喜欢的方式创建自己的项目文件。比如&#xff0c;使用premake等来…

作者头像 李华
网站建设 2026/2/22 14:30:18

pytesseract 中英文 识别图片文字

要使用 pytesseract 识别图片文字,你需要先安装 Tesseract OCR引擎 和 Pillow库,然后通过几行 Python 代码导入库、加载图片,并调用 image_to_string() 函数进行识别,传入图片路径和指定语言 (如 ‘eng’ 或 ‘chi_sim’) 即可获得文本内容。 步骤 1: 安装 Tesseract OCR引…

作者头像 李华