一、线性布局
定义:所有的控件在线性方向上依次排列。
语法:
android:orientation=" "
horizontal:水平的 vertical:竖直的 不设置属性,默认情况下:horizontal
注:水平时,宽度不能为match_parent
竖直时:高度不能为match_parent
layout_gravity和android:gravity的区别:
layout_gravity:空间在局中的对齐方式
gravity:文件在布局中的对齐方式
注:水平排列的时候只能要求竖直方向对齐,竖直排列的时候只能要求水平方向对齐。
android:layout_weight 指定控件的大小,和水平方向的控件按比例平分屏幕。
dp是android中用于指定控件大小间距等属性的单位
比较舒服的界面设计:
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type somrthing" />
<Button
android:id="@+id/butto1n"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
tools:ignore="MissingConstraints" />
二、相对布局(RelativeLayout)
定义:通过相对定位的方式让控件出现在布局中的任何位置
语法:
<RelativeLayout
常用的属性:
android:layout_above="@+id/name" //在id名为name的空间的上方
android:layout_below="@+id/name" //在id名为name的空间的下方
android:layout_toLeftof="@+id/name" //在id名为name的空间的左侧
android:layout_toRightof="@+id/name" //在id名为name的空间的右侧
android:layout_marginTop="5dp" //距离父控件的距离
对应的四个方向:Top Bottom Left Right
利用相对布局即属性设计的对应的登陆界面:
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:layout_marginLeft="30dp"
android:text="账号:"
android:textSize="24sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_marginTop="10dp"
android:layout_alignLeft="@+id/text1"
android:text="密码:"
android:textSize="24sp" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_alignLeft="@+id/text2"
android:layout_marginTop="10dp"
android:text="验证码:"
android:textSize="24sp" />
<EditText
android:layout_width="212dp"
android:layout_height="wrap_content"
android:hint="账号"
android:maxLength="11"
android:phoneNumber="true"
tools:ignore="MissingConstraints"
android:layout_marginTop="172dp"
android:layout_toRightOf="@+id/text1"
android:maxLines="1"/>
<EditText
android:layout_width="212dp"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_toRightOf="@+id/text2"
android:hint="密码"
android:password="true"
android:layout_marginTop="5dp"
android:maxLines="1"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/text33"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text3"
android:hint="验证码"
android:layout_marginTop="5dp"
android:maxLines="1"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/yanzheng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_toRightOf="@+id/text33"
android:layout_marginTop="10dp"
android:layout_centerInParent="true"
android:text="获取验证码" />
<Button
android:id="@+id/login"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_below="@+id/text3"
android:layout_marginTop="31dp"
android:layout_centerInParent="true"
android:text="登录" />
效果图如下:
三、帧布局<FrameLayout>
没有方便的定位方式,所有的空间都会默认的摆放在布局的左上角
(此布局并不经常使用,此处不做详细介绍)
四、百分比布局<PercentFrameLayout>
百分比布局属于新增布局, 是FrameLayout和RelativeLayout两种布局的扩展,允许直接指定控件在布局中所占的百分比。
为了保证保证百分比布局在Android所有系统版本上的兼容性,需要打开app/build.gradle文件,在dependencies闭包中添加如下内容:
dependencies{
compile fileTree(dir:'libs',include:['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:percent:24.2.1'
testCompile 'junit:junit:4.12'
}
android:layout_widthPercent="50%"
android:layout_heightPercent="50%"
android:layout_gravity="left|top"
android:layout_gravity="right|bottom"
<Button
android:id="@+id/button1"
android:layout_widthPercent="50%"
android:layout_heightPercent="50%"
android:layout_gravity="left|top"
android:text="Button1"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button2"
android:layout_widthPercent="50%"
android:layout_heightPercent="50%"
android:layout_gravity="left|bottom"
android:text="Button2"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button3"
android:layout_widthPercent="50%"
android:layout_heightPercent="50%"
android:layout_gravity="right|top"
android:text="Button3"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button4"
android:layout_widthPercent="50%"
android:layout_heightPercent="50%"
android:layout_gravity="right|bottom"
android:text="Button4"
tools:ignore="MissingConstraints" />
五、约束布局(ConstraintLayout)
作用:解决嵌套布局,按照比例约束控件的位置和大小, 可以更好的适配屏幕大小不同的机型
1.添加依赖
2.角度定位
3.相对定位
4.边距
5.尺寸约束