Android常用布局总结之(LinearLayout、GridLayout等4种)
yuyutoo 2024-10-19 11:08 12 浏览 0 评论
一、LinearLayout 线性布局
LinearLayout 是一个视图组,用于使所有子视图在单个方向(垂直或水平)保持对齐。您可以使用 android:orientation 属性指定布局方向。
- android:orientation,指定布局方向,vertical-竖向布局,horizontal-横向布局
- android:layout_weight,为各个子视图分配权重(数字)
- android:layout_gravity,容器自身相对于的上级容器的排列方式
- android:gravity,容器内部组件的排列方式(top,bottom,start,end,center)
<?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="match_parent"
android:layout_height="wrap_content"
android:text="文本标签1"
android:background="@color/teal_700"
android:textColor="@color/white"
android:textSize="60sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本标签2"
android:background="@color/teal_200"
android:textColor="@color/white"
android:textSize="60sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本标签3"
android:background="@color/purple_200"
android:textColor="@color/white"
android:textSize="60sp"
/>
</LinearLayout>
线性布局 - 均等分布
如果要让每个子视图使用大小相同的屏幕空间,请将每个视图的 android:layoutheight 设置为 "0dp"(针对垂直布局),或将每个视图的 android:layoutwidth 设置为 "0dp"(针对水平布局)。然后,将每个视图的 android:layout_weight 设置为 "1"。
<?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="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="文本标签1"
android:background="@color/teal_700"
android:textColor="@color/white"
android:textSize="60sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="文本标签2"
android:background="@color/teal_200"
android:textColor="@color/white"
android:textSize="60sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="文本标签3"
android:background="@color/purple_200"
android:textColor="@color/white"
android:textSize="60sp"
/>
</LinearLayout>
二、TableLayout 表格布局
1、TableLayout继承了LinearLayout
- android:stretchColumns,设置需要被隐藏的列的序号,从0开始,多个用,隔开
- android:collapseColumns,设置需要被隐藏的列的序号,从0开始,多个用,隔开
- android:shrinkColumns,设置允许被收缩的列的列序号,从0开始,多个用,隔开
2、子控件设置属性
- android:layout_span,横向跨几列
- android:layout_column,显示在第几列,从0开始
3、子控件TableRow,控制每行显示数据
4、如何确定行数与列数
- 如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!
- 如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!
- tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定
- tablerow的layoutwidth属性,默认是fillparent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrapten——content的,我们却可以自己设置大小!
- 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
- 有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:collapseColumns=""
android:stretchColumns="1"
android:shrinkColumns="2"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本1"
android:background="@color/teal_700"
android:textColor="@color/white"
android:textSize="40sp"
android:layout_span="1"
android:layout_column="0"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本2"
android:background="@color/teal_200"
android:textColor="@color/white"
android:textSize="40sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本3文本3文本3文本3"
android:background="@color/purple_200"
android:textColor="@color/white"
android:textSize="40sp"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本4"
android:background="@color/teal_700"
android:textColor="@color/white"
android:textSize="40sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本5"
android:background="@color/teal_200"
android:textColor="@color/white"
android:textSize="40sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本6"
android:background="@color/purple_200"
android:textColor="@color/white"
android:textSize="40sp"
/>
</TableRow>
</TableLayout>
三、GridLayout 网格布局
GridLayout和TableLayout(表格布局)有点类似,不过比TableLayout更加灵活好用。
- android:orientation,指定布局方向,vertical-竖向布局,horizontal-横向布局
- android:columnCount,设置总共多少列,默认没限制
- android:rowCount,设置总共多少行,默认没限制
- android:useDefaultMargins,true-如果组件没设置margin属性,则使用默认间距
子控件属性
- android:layout_columnSpan,设置组件横跨多列
- android:layout_rowSpan,设置组件横跨多行
- 如果设置了多行或多列,且你要让组件填满横越过的行或列的话,需要添加下面这个属性: android:layout_gravity = "fill"
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:rowCount="6"
android:useDefaultMargins="true">
<TextView
android:layout_columnSpan="4"
android:layout_gravity="fill"
android:gravity="end"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFCCCC"
android:text="0"
android:textSize="50sp" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="回退" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:padding="10sp"
android:text="清空" />
<Button android:text="+" />
<Button android:text="1" />
<Button android:text="2" />
<Button android:text="3" />
<Button android:text="-" />
<Button android:text="4" />
<Button android:text="5" />
<Button android:text="6" />
<Button android:text="*" />
<Button android:text="7" />
<Button android:text="8" />
<Button android:text="9" />
<Button android:text="/" />
<Button android:text="." />
<Button android:text="0" />
<Button android:text="=" />
</GridLayout>
四、RelativeLayout 相对布局
RelativeLayout 相对布局,可以相对父容器,也可以相对兄弟组件。
- android:gravity,容器内部组件的排列方式(top,bottom,start,end,center),可以同时设置竖向横向两个竖向,用|隔开,比如end|top(左上方)
- android:ignoreGravity,指定id的组件不受gravity影响
子控件属性
1)根据父容器定位
- android:layout_alignParentStart,左对齐
- android:layout_alignParentEnd,右对齐
- android:layout_alignParentTop,上对齐
- android:layout_alignParentBottom,下对齐
- android:layout_centerHorizontal,水平居中
- android:layout_centerVertical,垂直居中
- android:layout_centerInParent,正中间
2)根据兄弟组件定位
- android:layout_toStartOf,参考组件的左边
- android:layout_toEndOf,参考组件的右边
- android:layout_above,参考组件的上面
- android:layout_below,参加组件的下面
- android:layout_alignTop,对齐参考组件的上边界
- android:layout_alignBottom,对齐参考组件的下边界
- android:layout_alignStart,对齐参考组件的左边界
- android:layout_alignEnd,对齐参考组件的右边界
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- 参照组件,在容器中间位置 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn01"
android:textColor="@color/black"
android:layout_centerInParent="true"
android:text="参照物"/>
<!-- 下方,且和参照物左对齐,如果控件大小一样,左右对齐效果是一样的 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/btn01"
android:layout_above="@id/btn01"
android:text="正上方"/>
<!-- 上方,且和参照物左对齐,如果控件大小一样,左右对齐效果是一样的 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/btn01"
android:layout_alignTop="@id/btn01"
android:text="左边"/>
<!-- 右方,且和参照物下对齐,如果控件大小一样,上下对齐效果是一样的 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_toEndOf="@id/btn01"
android:layout_alignBottom="@id/btn01"
android:text="右边"/>
<!-- 右方,上方 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_toEndOf="@id/btn01"
android:layout_above="@id/btn01"
android:text="右上方"/>
<!-- 右方,下方 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_toEndOf="@id/btn01"
android:layout_below="@id/btn01"
android:text="右下方"/>
<!-- 左方,下方 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/btn01"
android:layout_below="@id/btn01"
android:text="左下方"/>
<!-- 左方,上方 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/btn01"
android:layout_above="@id/btn01"
android:text="左上方"/>
<!-- 下方,且和参照物左对齐 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/btn01"
android:layout_below="@id/btn01"
android:text="正下方"/>
</RelativeLayout>
相关推荐
- 自卑的人容易患抑郁症吗?(自卑会导致抑郁吗)
-
Filephoto[Photo/IC]Lowself-esteemmakesusfeelbadaboutourselves.Butdidyouknowthatovert...
- 中考典型同(近)义词组(同义词考题)
-
中考典型同(近)义词组...
- BroadcastReceiver的原理和使用(broadcast-suppression)
-
一、使用中注意的几点1.动态注册、静态注册的优先级在AndroidManifest.xml中静态注册的receiver比在代码中用registerReceiver动态注册的优先级要低。发送方在send...
- Arduino通过串口透传ESP 13板与java程序交互
-
ESP13---是一个无线板子,配置通过热点通信Arduino通过串口透传ESP13板与java程序交互...
- zookeeper的Leader选举源码解析(zookeeper角色选举角色包括)
-
作者:京东物流梁吉超zookeeper是一个分布式服务框架,主要解决分布式应用中常见的多种数据问题,例如集群管理,状态同步等。为解决这些问题zookeeper需要Leader选举进行保障数据的强一致...
- 接待外国人英文口语(接待外国友人的英语口语对话)
-
接待外国人英文口语询问访客身份: MayIhaveyourname,please? 请问您贵姓? Whatcompanyareyoufrom? 您是哪个公司的? Could...
- 一文深入理解AP架构Nacos注册原理
-
Nacos简介Nacos是一款阿里巴巴开源用于管理分布式微服务的中间件,能够帮助开发人员快速实现动态服务发现、服务配置、服务元数据及流量管理等。这篇文章主要剖析一下Nacos作为注册中心时其服务注册与...
- Android面试宝典之终极大招(android面试及答案)
-
以下内容来自兆隆IT云学院就业部,根据多年成功就业服务经验,以及职业素养课程部分内容,归纳总结:18.请描述一下Intent和IntentFilter。Android中通过Intent...
- 除了Crontab,Swoole Timer也可以实现定时任务的
-
一般的定时器是怎么实现的呢?我总结如下:1.使用Crontab工具,写一个shell脚本,在脚本中调用PHP文件,然后定期执行该脚本;2.ignore_user_abort()和set_time_li...
- Spark源码阅读:DataFrame.collect 作业提交流程思维导图
-
本文分为两个部分:作业提交流程思维导图关键函数列表作业提交流程思维导图...
- 使用Xamarin和Visual Studio开发Android可穿戴设备应用
-
搭建开发环境我们需要做的第一件事情是安装必要的工具。因此,你需要首先安装VisualStudio。如果您使用的是VisualStudio2010,2012或2013,那么请确保它是一个专业版本或...
- Android开发者必知的5个开源库(android 开发相关源码精编解析)
-
过去的时间里,Android开发逐步走向成熟,一个个与Android相关的开发工具也层出不穷。不过,在面对各种新鲜事物时,不要忘了那些我们每天使用的大量开源库。在这里,向大家介绍的就是,在这个任劳任怨...
- Android事件总线还能怎么玩?(android实现事件处理的步骤)
-
顾名思义,AndroidEventBus是一个Android平台的事件总线框架,它简化了Activity、Fragment、Service等组件之间的交互,很大程度上降低了它们之间的耦合,使我们的代码...
- Android 开发中文引导-应用小部件
-
应用小部件是可以嵌入其它应用(例如主屏幕)并收到定期更新的微型应用视图。这些视图在用户界面中被叫做小部件,并可以用应用小部件提供者发布。可以容纳其他应用部件的应用组件叫做应用部件的宿主(1)。下面的截...
你 发表评论:
欢迎- 一周热门
-
-
前端面试:iframe 的优缺点? iframe有那些缺点
-
带斜线的表头制作好了,如何填充内容?这几种方法你更喜欢哪个?
-
漫学笔记之PHP.ini常用的配置信息
-
推荐7个模板代码和其他游戏源码下载的网址
-
其实模版网站在开发工作中很重要,推荐几个参考站给大家
-
[干货] JAVA - JVM - 2 内存两分 [干货]+java+-+jvm+-+2+内存两分吗
-
正在学习使用python搭建自动化测试框架?这个系统包你可能会用到
-
织梦(Dedecms)建站教程 织梦建站详细步骤
-
【开源分享】2024PHP在线客服系统源码(搭建教程+终身使用)
-
2024PHP在线客服系统源码+完全开源 带详细搭建教程
-
- 最近发表
-
- 自卑的人容易患抑郁症吗?(自卑会导致抑郁吗)
- 中考典型同(近)义词组(同义词考题)
- WPF 消息传递简明教程(wpf messagebox.show)
- BroadcastReceiver的原理和使用(broadcast-suppression)
- Arduino通过串口透传ESP 13板与java程序交互
- zookeeper的Leader选举源码解析(zookeeper角色选举角色包括)
- 接待外国人英文口语(接待外国友人的英语口语对话)
- 一文深入理解AP架构Nacos注册原理
- Android面试宝典之终极大招(android面试及答案)
- 除了Crontab,Swoole Timer也可以实现定时任务的
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)