当前位置: 首页 > news >正文

基本布局(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度)
http://www.cnnetsun.cn/news/53375.html

相关文章:

  • 【学习笔记】如果打造可复现、可评测、可迭代的AI技术体系
  • 【论文自动阅读】See Once, Then Act: Vision-Language-Action Model with Task Learning from One-Shot Video Demo
  • 利用齐次坐标系证明各种几何定理【射影几何】
  • 小程序基于springboot的乡镇普法知识科普宣传系统 律师预约系统设计与实现_qf4cwws6(java毕业设计项目源码)
  • 面向对象编程三大特性:封装、继承、多态的核心要义
  • leetcode 2147. 分隔长廊的方案数 困难
  • 学生党必备!这款桌面课表工具太省心了
  • 深度学习实验14代码
  • 优化及性能-–-behaviac
  • 练题100天——DAY26:汇总区间+丢失的数字+数组交集
  • 当AI芯片不再性感:博通的高增长,为何成了催命符?
  • Vibe Coding:AI驱动的编程新范式
  • AI 数字孪生工厂:西门子与中信特钢的实践,如何降本 11%?
  • Spring IoC的实现机制是什么?
  • 耐用折叠屏手机推荐:三星Galaxy Z TriFold如何破解“折痕与耐用”难题?
  • 前端技术风险防控:以防为主,防控结合
  • 给女神发“在吗”,她回了个表情包是几个意思?—— 硬核探讨TCP 三次握手
  • 入门大模型必知的100个基础问题(附简明答案)
  • vue基于Spring Boot的建筑材料管理系统的应用和研究_ug8y52z3
  • 【大模型】-LangChain--RAG文档系统
  • 探索非线性电液伺服系统的模型自适应反步控制
  • 降AI率就要牺牲文笔?WriterPro第一个不服!实测对比比原文写得还好,这文笔简直绝了
  • 我不是这样
  • 10.8 总结
  • 列车售票|基于springboot 列车售票系统(源码+数据库+文档)
  • AI驱动的手动测试变革:赋能而非替代
  • 【奶茶Beta专项】【LVGL9.4源码分析】09-core-group
  • 网络安全异想天开(不定期更新)
  • 《CAPL脚本实现CANOE工具 Bus-Off自动恢复(含重试机制)》
  • 力扣1965-丢失信息的雇员