书小宅之android开发——4种基本布局

本文详细介绍了Android中的各种布局方式,包括线性布局、相对布局、帧布局及百分比布局的使用方法和属性设置,如android:gravity、android:layout_gravity、android:layout_weight等,帮助开发者掌握控件在不同布局中的显示和对齐。

android:gravity:用于指定文字在控件中的对其方式;
android:layout_gravity:用于指定控件在布局中的对齐方式,多属性用“|”来分隔。
android:layout_weight:允许使用比例的方式指定控件的大小

dp是Android中用于指定控件大小、间距等属性的单位。

线性布局LinearLayout

android:orienttation="vertical"//控件在垂直方向上排列
//内部的控件高度不能match_parent
//垂直方向上的长度不固定,只有水平方向上的对齐方式才会生效
android:orienttation="horizontal"//控件在水平方向上排列,默认属性
//内部的控件宽度不能match_parent
//水平方向上的长度不固定,只有垂直方向上的对齐方式才会生效
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orienttation="horizontal"
	android:layout_width="match_parent"
	android:layout_height="match_parent">

	<EditText
		android:id="@+id/input_message"
		android:layout_width="0dp"
		android:layout_height="wrap_content"
		android:layout_weight="1"
		android:hint="Type something" />

	<Button
		android:id="@+id/send"
		android:layout_widgh="wrap_content"
		android:layout_weight="wrap_content"
		android:text="Send" />
</LinearLayout>

相对布局RelativeLayout

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

//相对于父布局的对齐方式,可以组合使用
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
//相对于控件进行定位,可以组合使用
android:layout_centerInParent="true"
android:layout_above="@id/button3"
android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"
android:layout_toRightOf="@id/button3"
//相对于控件边缘对齐,可组合使用
android:layout_alignLeft="true"
android:layout_alignRight="true"
android:layout_alignTop="true"
android:layout_alignBottom="true"

帧布局FrameLayout

控件默认摆放在布局的左上角,用android:layout_gravity属性指定控件在布局中的对齐方式。

百分比布局

直接指定控件在布局中所占的百分比,是为帧布局和相对布局扩展的布局——PercentFrameLayout和PercentRelativeLayout。
百分比布局定义在support库中,在build.gradle文件的dependencies闭包中添加百分比库的依赖,之后点击Sync Now进行同步

compile 'com.android.support:percent:24.2.1'
<android.support.percent.PercentFrameLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	android:layout_width="match_parent"
	android:layout_height="match_parent">

	<Button
		android:id="@+id/button2"
		android:text="Button_2"
		android:layout_gravity="right|top"
		android:widthPercent="50%"
		android:heightPercent="50%" /> 
		
	<Button
		android:id="@+id/button3"
		android:text="Button_3"
		android:layout_gravity="left|bottom"
		android:widthPercent="50%"
		android:heightPercent="50%" /> 
		
	<Button
		android:id="@+id/button4"
		android:text="Button_4"
		android:layout_gravity="right|bottom"
		android:widthPercent="50%"
		android:heightPercent="50%" /> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值