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
0
投稿
猜你喜欢
- 本文实例讲述了Spring实战之SpEl语法。分享给大家供大家参考,具体如下:一 Beanpackage org.crazyit.app.d
- import java.io.File;import java.io.FileNotFoundException;import java.i
- 本文实例为大家分享了java实现通过绑定邮箱找回密码功能,供大家参考,具体内容如下1.输入用户名及验证码,验证用户名是否存在(1).生成验证
- 奇怪的不等于(≠)最近,栈长用 IntelliJ IDEA 看源码时发现:咦~这是什么鬼?Java 不等于的写法不是一直都是 != 么?什么
- 前文传送门:NioEventLoop处理IO事件执行任务队列继续回到NioEventLoop的run()方法:protected void
- 本文实例讲述了Java中final与继承操作。分享给大家供大家参考,具体如下:一 点睛final在Java中被称为终结器。1 在基类的某个方
- Android 自定义组件成JAR包的实现方法,这里对自己实现的Android View 组件进行JAR 包的处理。
- 概述附件,指随同文件发出的有关文件或物品。在PDF文档中,我们可以添加同类型的或其他类型的文档作为附件内容,而PDF中附件也可以分为两种存在
- 最近刚完成一个简单的网络爬虫,开始的时候很迷茫,不知道如何入手,后来发现了很多的资料,不过真正能达到我需要,有用的资料--代码很难找。所以我
- 前言在RocketMQ中为,我们创建消息生产者时,只需要设置NameServer地址,消息就能正确地发送到对应的Broker中,那么Rock
- 新手当在一个类文件中进行了一些操作之后,会造成sout快捷命令无法自动生成。比如操作了import引入其它包之后。主要是对IDEA操作的不熟
- 最近项目里有用到ViewPager来做广告运营位展示,看到现在很多APP的广告运营位都是无限循环的,所以就研究了一下这个功能的实现。先看看效
- (注意:本文基于JDK1.8)前言任何一个容器类对象用于持有元素后,总是需要遍历元素的,即挨个去访问每个元素1次,而遍历元素,除了常规的依赖
- 本文实例为大家分享了Java实现图形界面计算器的具体代码,供大家参考,具体内容如下 代码:import javax.swing.*
- c#开发cad如何预览图块1.定义变量的方法代码如下2. 获取GetDwgImag图像的方法代码3.实现显示DWG文件的方法代码方
- 一、简介日志打印是java代码开发中不可缺少的重要一步。日志可以排查问题,可以搜集数据二、常用日志框架比较常用的日志框架就是logback,
- Android 短信验证码自动填写1.自定义Observer监听短信数据库变化(注意添加短信权限)import android.app.Ac
- 参考ColorComboBox做修改,并对颜色名做些修正,用于CR MVMixer产品中,聊作备忘~效果图:代码://颜色拾取框using
- 一、什么是设计模式设计模式(Design pattern) 是解决软件开发某些特定问题而提出的一些解决方案也可以理解成解决问题的一
- 之前有学弟问过我一道java的面试题,题目不算难。用java实现杨辉三角。我花了点时间整理了一下,发现挺有意思的,于是想写下来分享一下。在写