安卓四大布局

线性布局:LinearLayout

重要属性

线性排列的方向:orientation属性

//水平线性排列(不指定orientation值时,默认为水平排列)
android:orientation="horizontal"

//垂直线性排列
android:orientation="vertival"

对齐方式:layout_gravity属性

常用值:

//顶部
android:layout_gravity="top"

//居中
android:layout_gravity="center"

//底部
android:layout_gravity="bottom"

注意:

  1. android:gravity用于指定文字在控件中的对齐方式,而android:layout_gravity用于指定控件在布局中的对齐方式
  2. 线性排列的方向为horizontal时,只有垂直方向上的对齐方式才会生效;线性排列的方向为vertical时,只有水平方向上的对齐方式才会生效

按比例指定控件的大小:layout_weight属性

//让EditText控件和Button控件大小按3:2水平分布(只包含部分和描述相关的代码)
<LinearLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
android:orientation="horizontal"
<EditText
android:id="@+id/input_message"
android:layout_width="0dp"
android:layout_weight="3"
/>
<Button
android:id="@+id/send"
android:layout_width="0dp"
android:layout_weight="2"
/>
/>

相对布局:RelativeLayout

通过相对定位的方式让控件出现在布局的任何位置。

相对于父布局进行定位

//用法示例:将控件排列在父布局的左上角
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"

相对于控件进行定位

//用法示例:将控件button1排列在控件button2的左上角
android:layout_above="@id/button2"
android:layout_toLeftOf="@id/button2"

注意:要先定义好被引用的控件button2,不然会出现找不到id的情况

帧布局:FrameLayout

所有控件都默认摆放在布局的左上角,同样可以用layout_gravity属性来指定控件在布局中的对齐方式。

由于定位方式的欠缺,应用场景少。

百分比布局:PercentFrameLayout/PercentRelativeLayout

原有的相对布局和帧布局方式不支持按比例指定控件大小,百分比布局不再需要使用wrap_content/match_parent等方式来指定控件大小,而是允许直接指定控件在布局中所占的百分比,可以实现按任意比例分割布局的效果。

注意:百分比布局不是内置在系统SDK中的,需要把完整的包路径写出来,并且定义一个app的命名空间

//示例:PercentFrameLayout:将button控件放在界面左上角,同时,高设为20%,宽设为30%
<android.support.percent.PercentFrameLayout 
		xmlns:android="<http://schemas.android.com/apk/res.android>"
		xlmns:app="<http://schemas.android.com/apk/res-auto>">
<Button
android:id="@+id/button"
android:text="button"
android:layout_gravity="left|top"
app:layout_widthPercent="30%"
app:layout_heightPercent="20%" />
</android.support.percent.PercentFrameLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IP_FF02

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值