Android - 布局

几篇关于布局的文章:

Android 五大布局讲解与应用 http://www.codeceo.com/article/android-5-layout-usage.html
约束布局:
Android 约束布局(ConstraintLayout)详解https://blog.csdn.net/airsaid/article/details/79052011
带你了解Android约束布局ConstraintLayout https://www.jianshu.com/p/ac450f6386ee
实现一个常用的底部导航栏,5个导航栏
https://www.jianshu.com/p/e49b8dcfd6bc

安卓的几种布局方式

1、线性布局(LinearLayout):按照垂直或者水平方向布局的组件
2、相对布局(RelativeLayout):相对其它组件的布局方式
3、帧布局(FrameLayout):组件从屏幕左上方布局组件
4、表格布局(TableLayout):按照行列方式布局组件
5、绝对布局(AbsoluteLayout):按照绝对坐标来布局组件
6、约束布局 (ConstraintLayout):按照约束布局组件 (谷歌2016年推出)

关于如何拖拽使用ConstraintLayout,可以去看下郭霖大神写的:Android新特性介绍,ConstraintLayout完全解析

布局管理器都是以ViewGroup为基类派生出来的; 使用布局管理器可以适配不同手机屏幕的分辨率,尺寸大小;

####父容器与本容器属性 :
android_layout…属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器都有一个LayoutParams内部类,
android:… 是父容器用来控制子组件的属性.
android:layout_gravity 是控制组件本身的对齐方式
android:gravity是控制本容器子组件的对齐方式;

1.线性布局(LinearLayout)

线性布局,最常用的布局之一,所有包含在线性布局里的控件在线性方向上依次排列。线性布局不会换行,当组件一个挨着一个地排列到头之后,剩下的组件将不会被显示出来。

1.方向
在线性布局里面的控件,是按照线性的顺序进行排列的,方向有两种:横向和纵向。
属性和属性值:
android:orientation = "horizontal" // 水平
android:orientation = "vertical" // 垂直
2.对齐方式
属性:
android:gravity //指本元素的子元素相对它的对齐方式
android:layout_gravity //指本元素相对它的父元素的对齐方式

注意:
相对其他属性,如果加上layout_前缀,就代表本元素相对父元素的属性。
常用属性值:
android:gravity = "center_horizontal" //子控件水平方向居中
android:gravity="center_vertical" //子控件竖直方向居中
android:gravity="center" //子控件竖直方向和水平方向居中
android:gravity= start || end || top || bottom //子控件左对齐 || 右对齐 || 顶部对齐 || 底部对齐
android:gravity= left || right  //子控件左对齐 || 右对齐

3.子控件大小
属性:
android:layout_height
android:layout_width
android:weight // 指定该子元素在LinearLayout中所占的权重

属性值:
android:layout_height = "wrap_content"//根据子控件内容的大小决定大小
android:layout_height = "match_parent" //子控件填满父容器
android:layout_height = "10dp" // 直接赋值
android:layout_weight = "1" //设置占比例为1,子控件占父控件的比例

2.相对布局(RelativeLayout)

相对布局可以让子控件相对于兄弟控件或父控件进行布局,可以设置子控件相对于兄弟控件或父控件进行上下左右对齐。相对布局就是一定要加Id才能管理。现在使用约束布局替代相对布局

1.常用属性值设置

相对于父控件
居中:
android:layout_alignParentTop=“true”
android:layout_centerHrizontal  //水平居中
android:layout_centerVertical //垂直居中

android:layout_centerInparent //相对于父元素完全居中
android:layout_alignParentBottom //贴紧父元素的下边缘
android:layout_alignParentLeft //贴紧父元素的左边缘
android:layout_alignParentRight //贴紧父元素的右边缘
android:layout_alignParentTop //贴紧父元素的上边缘 

2.子控器间的设置
根据另一个控件的位置来确定控件的位置:
android:layout_below //在某元素的下方
android:layout_above //在某元素的上方
android:layout_toLeftOf // 在某元素的左边
android:layout_toRightOf //在某元素的右边
android:layout_alignTop //本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft //本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom //本元素的下边缘和某元素的的下边缘对齐    
android:layout_alignRight //本元素的右边缘和某元素的的右边缘对齐

3.内边距和外边距设置
margin 边缘(外):指该控件距离父控件或其他控件的边距
android:layout_marginBottom //离某元素底边缘的距离
android:layout_marginLeft //离某元素左边缘的距离
android:layout_marginRight //离某元素右边缘的距离
android:layout_marginTop //离某元素上边缘的距离

padding 衬垫,填充(内):指该控件内部内容,如文本/图片距离该控件的边距。
android:padding //为组件的四边设置相同的内边距。
android:paddingLeft //为组件的左边设置内边距。
android:paddingRight //为组件的右边设置内边距。
android:paddingTop //为组件的上边设置内边距。
android:paddingBottom //为组件的下边设置内边距

3. 帧布局(FrameLayout)

帧布局或叫层布局,从屏幕左上角按照层次堆叠方式布局,后面的控件覆盖前面的控件。
该布局在开发中设计地图经常用到,因为是按层次方式布局,我们需要实现层面显示的样式时就可以
采用这种布局方式,比如我们要实现一个类似百度地图的布局,我们移动的标志是在一个图层的上面。
在普通功能的软件设计中用得也不多。层布局主要应用就是地图方面。

特有属性
android:foreground //设置改帧布局容器的前景图像
android:foregroundGravity //设置前景图像显示的位置

4.表格布局(TableLayout)

表格布局就是让控件以表格的形式来排列控件,只要将控件放在单元格中,控件就可以整齐地排列,使用TableLayout标签。
TableLayout继承了 LinearLayout,因此它的本质依然是线性布局管理器。

表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线性布局,且所以子元素宽度和高度一致。
注意:在TableLayout中,单元格可以为空,但是不能跨列,意思是只能不能有相邻的单元格为空。
TableLayout常用属性:
android:shrinkColumns:设置可收缩的列,内容过多就收缩显示到第二行
android:stretchColumns:设置可伸展的列,将空白区域填充满整个列
android:collapseColumns:设置要隐藏的列
列的索引从0开始,shrinkColumns和stretchColumns可以同时设置。
子控件常用属性:
android:layout_column:表示当前控件在第几列
android:layout_span:占据列数

5.绝对布局(AbsoluteLayout)

绝对布局需要通过指定x、y坐标来控制每一个控件的位置,放入该布局的控件需要通过android:layout_x和android:layout_y 两个属性指定其准确的坐标值,并显示在屏幕上。

使用绝对布局时,每个子组件都可指定如下两个XML属性。
layout_x //指定该子组件的X坐标。
layout_y //指定该子组件的Y坐标。

6.约束布局 (ConstraintLayout)

根据布局中的其他元素或视图, 确定View在屏幕中的位置, 受到三类约束, 即其他视图, 父容器(parent), 基准线(Guideline)
它可以有效地解决布局嵌套过多的问题;
控件拖动添加约束;
约束控件最低支持的版本是 Android 2.3 (Gingerbread)

相对位置的属性 ,这些属性的值可以是parent,也可以是某个view的id。
layout_constraintTop_toTopOf    // 将所需视图的顶部与另一个视图的顶部对齐。 
layout_constraintTop_toBottomOf  // 将所需视图的顶部与另一个视图的底部对齐。 
layout_constraintBottom_toTopOf  // 将所需视图的底部与另一个视图的顶部对齐。 
layout_constraintBottom_toBottomOf // 将所需视图的底部与另一个视图的底部对齐。 
layout_constraintLeft_toTopOf   // 将所需视图的左侧与另一个视图的顶部对齐。 
layout_constraintLeft_toBottomOf  // 将所需视图的左侧与另一个视图的底部对齐。 
layout_constraintLeft_toLeftOf   // 将所需视图的左边与另一个视图的左边对齐。 
layout_constraintLeft_toRightOf  // 将所需视图的左边与另一个视图的右边对齐。 
layout_constraintRight_toTopOf   // 将所需视图的右对齐到另一个视图的顶部。
layout_constraintRight_toBottomOf // 将所需视图的右对齐到另一个的底部。
layout_constraintRight_toLeftOf  // 将所需视图的右边与另一个视图的左边对齐。
layout_constraintRight_toRightOf  // 将所需视图的右边与另一个视图的右边对齐。

比例;
layout_constraintDimensionRatio="2:1" //宽高比 2:1
layout_constraintLeft_toLeftOf="parent"
layout_constraintRight_toRightOf="parent"/>
layout_constraintHeight_percent://高度百分比,占父类高度的百分比
layout_constraintWidth_percent://宽度百分比,占父类宽度的百分比

app:layout_constraintWidth_default="percent" //设置宽为百分比
app:layout_constraintWidth_percent="0.3" //0到1之间的值
或
app:layout_constraintHeight_default="percent" //设置高为百分比
app:layout_constraintHeight_percent="0.3" //0到1之间的值


layout_constraintVertical_bias://垂直是垂直偏移率。
layout_constraintHorizontal_bias://水平水平偏移率。

app:layout_constraintHorizontal_weight //水平权重
app:layout_constraintVertical_weight //竖直权重

别人写的一个例子

image

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="验证码"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="手机号"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="197dp"
        android:layout_height="39dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="请输入验证码"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/editText3" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="298dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="请输入手机号"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="获取验证码"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText3" />

    <Button
        android:id="@+id/button3"
        android:layout_width="358dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="开始"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />
</android.support.constraint.ConstraintLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值