Android学习笔记-LinearLayout-线性布局
Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局)
用到最多的是前两种:LinearLayout(线性布局),RelativeLayout(相对布局)
先试试线性布局:
<LinearLayout
android:layout_width="200dp"
设置区域宽度 200dp表示200个单位的宽度 dp是这里常用的单位
android:layout_height="match_parent"
设置区域高度 match_parent 代表适应屏幕的高度(或者宽度)
android:orientation="vertical"
代表在垂直方向上布局
android:background="#66CCFF"
设置区域颜色
android:paddingLeft="40dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="30dp"
上面这四个分别代表区域内的子区域至少要距离此区域边界的长度
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
这两个代表区域至少距离屏幕的距离 可以看到至少要距离15dp
>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccff00"
/>
<View/>内部的代码是其子区域的属性,match_parent即为匹配边界最大值,那么子区域的大小则由上面四个padding决定
</LinearLayout>
这段代码预览如图,可以看到高度是适应了屏幕的高度,宽度是200,约一半,内部子区域距离边界分别是40dp20dp10dp30dp,
并且左侧距离边界至少为15dp
我们把代码扩充一下,最后变成这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#66CCFF"
android:paddingLeft="40dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="30dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccff00"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66CC33"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="30dp">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ffff00" />
</LinearLayout>
</LinearLayout>
然后就变成这样的了,是同样的道理
我们再看下一个例子
<LinearLayout
android:layout_width="350dp"
android:layout_height="200dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="#66CCFF"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ccff00" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0033" />
</LinearLayout>
这里的关键点在android:layout_weight="1",这个weight是权重的意思,它的意思是,
除开本身数据的dp之后剩下的区域会根据两个区域的权重比值分配(可以看到两个区域的本来的宽度其实是零),可以看到我这里是变成了平分,
因为他们的权重都是1,是相等的,如果把第二个改成2的权重,就变成下一张图的样子了
作者:冰稀饭Aurora
出处:https://www.cnblogs.com/rsy-bxf150/p/17149220.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!