Kotlin LinearLayout与RelativeLayout布局使用详解
作者:go2coding 时间:2021-12-06 02:07:30
安卓的开发从布局开始。
安卓的界面编写也是使用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
标签:Kotlin,LinearLayout,RelativeLayout
0
投稿
猜你喜欢
漂亮的Android音乐歌词控件 仿网易云音乐滑动效果
2022-01-26 14:16:25
关于后端如何解决跨域的问题说明
2023-09-19 00:59:10
SpringBoot+Nacos+Kafka微服务流编排的简单实现
2023-03-21 10:34:53
java读取其他服务接口返回的json数据示例代码
2023-11-10 14:05:29
C#使用OpenCv图像批处理并改变图片大小并且重命名
2023-12-20 11:10:29
Java使用Collections.sort()排序的方法
2022-09-11 18:47:55
浅析C# 状态机Stateless
2022-03-08 16:39:07
Android PopupWindow被输入法弹上去之后无法恢复原位的解决办法
2023-07-24 12:23:31
Java中Set集合的使用和底层原理解析
2022-12-16 20:54:28
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
Android四种数据存储的应用方式
2023-07-25 05:01:06
Junit测试多线程无法得到结果的问题解决
2023-01-01 22:12:52
基于C#实现Windows服务状态启动和停止服务的方法
2022-07-27 12:17:57
java算法题解牛客BM99顺时针旋转矩阵示例
2021-07-08 18:55:18
Java数据结构与算法之稀疏数组与队列深入理解
2022-05-31 06:33:11
详解Java中Math.round()的取整规则
2022-10-20 14:22:01
Android UI控件ExpandableListView基本用法详解
2021-12-26 22:20:50
java日期操作工具类(获取指定日期、日期转换、相隔天数)
2023-11-28 06:42:53
C#中分部类和分部方法的应用
2022-08-16 06:49:05
springboot aspect通过@annotation进行拦截的实例代码详解
2023-10-15 20:12:54