一、线性布局(LinearLayout)
注意:如果排列方向为horizontal,则不可以将宽度设置为match_parent,因为一个控件就占据了全部空间,其他控件就没有地方放了;
同理,如果排列方向为vertical,则不可以将高度设置为match_parent
layout_gravity属性设置布局
android:layout_weight属性:
允许使用比例的方式来指定控件的大小,在手机屏幕适配性上起很大作用,下面编写一个文本编辑框和一个发送按钮
将文本框和按钮的layout_weight的属性都设置为1,意思是两个控件各占二分之一宽度
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Type something"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="send"
/>
仅仅指定EditText的layout_weight属性,将Button的宽度改为wrap_content,这时文本框将占据除按钮外的全部宽度
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Type something"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
/>
二、相对布局(RelativeLayout)
可以通过相对定位的方式让空间出现在布局的任意位置
1.相对于父布局定位
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="button1"/>
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="button2"/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="button3"/>
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="button4"/>
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="button5"/>
2.相对控件定位
需要注意的是基准的控件要写在最前面,不然会报错
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="button2"/>
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_2"
android:layout_toLeftOf="@+id/button_2"
android:text="button1"/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_2"
android:layout_toRightOf="@+id/button_2"
android:text="button3"/>
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_2"
android:layout_toRightOf="@+id/button_2"
android:text="button4"/>
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_2"
android:layout_toLeftOf="@+id/button_2"
android:text="button5"/>
3.另一种相对控件定位的属性
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="button2"/>
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button_2"
android:layout_below="@+id/button_2"
android:text="button1"/>
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button_2"
android:text="button3"/>
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button_2"
android:layout_toRightOf="@+id/button_2"
android:text="button4"/>
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button_2"
android:text="button5"/>
三、帧布局(FrameLayout)
没有方便的定位方式,所有控件都会默认摆放在布局的左上角
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.a102.uilayouttest.FrameActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!!!!!!!!!!!!"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</FrameLayout>
由于都在左上角,文本被图片挡住了,这个是默认效果,可以使用layout_gravity属性来指定控件位置
四、百分比布局
允许直接指定控件在布局中所占的百分比
由于LinearLayout布局已经支持按比例指定控件大小了,所以这个布局用来扩展其他的两个布局。提供了PercentFrameLayout、PercentRelativeLayout两个全新的布局
首先添加依赖,在app->build.gradle里的dependencies下添加
compile 'com.android.support:percent:24.2.1'
编辑布局,PercentFrameLayout会继承FrameLayout布局,所有控件会摆放在左上角,所以采用gravity属性分开控件
<Button
android:id="@+id/button_1"
android:layout_gravity="left|top"
android:text="Button1"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
/>
<Button
android:id="@+id/button_2"
android:layout_gravity="right|top"
android:text="Button2"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
/>
<Button
android:id="@+id/button_3"
android:layout_gravity="left|bottom"
android:text="Button3"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
/>
<Button
android:id="@+id/button_4"
android:layout_gravity="right|bottom"
android:text="Button4"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
/>
自定义View的xml布局文件中需要加入xmlns:前缀=http://schemas.android.com/apk/res/你的应用程序包路径
xmlns:app="http://schemas.android.com/apk/res-auto"