Kotlin LinearLayout与RelativeLayout布局使用详解
作者:go2coding 发布时间:2021-12-06 02:07:30
标签:Kotlin,LinearLayout,RelativeLayout
安卓的开发从布局开始。
安卓的界面编写也是使用xml
进行布局的,一般如果熟悉了html
界面的布局,那么很容易就能够理解安卓有关的布局了,这里介绍两个比较重要的布局方式:线性布局(LinearLayout)和相对布局(RelativeLayout)。
新建的功能布局,一般是一个界面对应一个xml
文件,main
界面的xml
在activity_main.xml
中。
线性布局LinearLayout
根据名字我们就很清楚,线性布局的意思了,相当于html中的div层,两种布局方向:
vertical
下的布局方式:
horizontal
下的布局方式:
vertical
布局代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#00aaff" >
<Button
android:id = "@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<Button
android:id = "@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
horizontal
下的布局代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#A6A7AF" >
<Button
android:id = "@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Button" />
<Button
android:id = "@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Button" />
</LinearLayout>
有几个属性需要熟悉一下:
wrap_content 为按照控件内容的大小进行调整
layout_marginLeft 为控件左边的偏移,其他的一次类推
layout_gravity 可以用来进行控件居中显示
layout_weight 控件在horizontal模式下占到的比率
相对布局RelativeLayout
相对布局 主要两种相当模式,一种是父控件,一种是相对兄弟控件。
布局代码如下:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#9C27B0" >
<Button
android:id = "@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Button5" />
<Button
android:id = "@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button6" />
<Button
android:id = "@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@+id/btn6"
android:text="Button7" />
</RelativeLayout>
几个重要的布局:
layout_alignParentxxxx 相对于父类的情况
layout_to 相对于兄弟的情况
项目在github的地址在这里。
小结
布局的方式比较多,但是这两个种布局方式是最重要的,也可以这么说掌握了这两种以后,其他的就可以依次类推,只要知道里面的属性基本上就容易上手了。
来源:https://blog.csdn.net/weixin_40425640/article/details/127850722


猜你喜欢
- Unity没有像Vs那样的“*.sln”的项目工程文件,不能通过这个文件来打开工程。但是原有的打开已有工程的方法太过于麻烦了,则现在来通过添
- 目录前言:1.委托的声明1.1.delegate1.1.1. 0-23个参数,可以有返回值也可以没有返回值1.1.2.委托的调用1.1.3.
- 1. 什么是JWTJSON Web Token(JWT)是一个轻量级的认证规范,这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信
- 前言古语有云:道为术之灵,术为道之体;以道统术,以术得道。其中:“道”指“规律、道理、理论”,“术”指“方法、技巧、技术”。意思是:“道”是
- 前言:顺序表的问题及思考1. 顺序表中间/头部的插入删除,时间复杂度为O(N)2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗
- 项目已经添加了svn,但右键项目时找不到Svn选择但在VCS中却有,很奇怪这个问题是svn的根路径与当前IDEA打开的项目路径不一致的原因在
- 前言哈喽,我是小黑, 最近学了java的输入输出流后一直心痒痒,总想找一点事情来做,所以用java代码来实现了一下统计代码的所有行数,看一下
- 一、介绍Spring是通过任务执行器(TaskExecutor)来实现多线程和并发编程,使用Spring提供的ThreadPoolTaskE
- 两种android图片裁剪方式,供大家参考,具体内容如下一、相机拍完照之后利用系统自带裁剪工具进行截取public static void
- 这篇文章主要介绍了Java 使用Calendar类输出指定年份和月份的日历,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 本文实例为大家分享了Java实现图形界面计算器的具体代码,供大家参考,具体内容如下 代码:import javax.swing.*
- 1.问题描述在一个目录及子目录下查找 TXT或Java文件,从中搜索所有“对象”字样的行。在D盘中的所有文件中搜索含有“对象”的行。2.解题
- 前言我们可能听过C语言中的传值和传指针,在其他语言中,也有传引用一说,那么他们到底有什么区别呢?如果你还不能准确地分辨,就该好好了解一下了。
- HttpClient介绍HttpClient 不是一个浏览器。它是一个客户端的 HTTP 通信实现库。HttpClient的目标是发 送和接
- 实现步骤:工具:IDEA数据库版本:mysql5.7一、环境搭建1.创建springboot项目pom.xml2.pom.xml : spr
- 自定义log4j日志文件命名规则项目中的日志需要采用一致的命名规范和文件规范,命名规则为:项目模块标识_index_日期时间_日志级别.lo
- java try catch异常后还会继续执行吗catch中如果你没有再抛出异常,那么catch之后的代码是可以继续执行的,但是try中,报
- 先给大家展示下效果图:1、验证码生成类:import java.util.Random;import java.awt.imag
- //执行顺序:(优先级从高到低。)静态代码块>mian方法>构造代码块>构造方法。其中静态代码块只执行一次。构造代码块在每
- Spring Boot如何实现配置文件的自动加载和刷新?在使用Spring Boot开发应用程序时,配置文件是非常重要的组成部分。在不同的环