【Android开发】安卓四大布局方式详解

心得感悟

刚开始觉得内容很多,感觉有点记不住,但后面理解起来还是比较容易的。这几天学的内容都有点多,目前还有些吃不消,后面自己慢慢消化吧。下午还一起写了密码解锁Demo,通过这个Demo,真的可以加深对本章内容的印象。


内容简概

  • 一、安卓布局方式总览
  • 二、FrameLayout(框架布局/帧布局)
  • 三、 LinearLayout(线性布局)
  • 四、 RelativeLayout(相对布局)
  • 五、ConstraintLayout(约束布局)

具体内容

一、安卓布局方式总览

①AbsoluteLayout(绝对布局)
②FrameLayout(框架布局/帧布局)
③LinearLayout(线性布局)
④RelativeLayout(相对布局)
⑤TableLayout(表格布局)
⑥ConstraintLayout(约束布局)
在这里暂不介绍(1)和(5)

二、FrameLayout(框架布局/帧布局)

据说这种布局方式在六大布局中最为简单,这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式,所以它应用的场景并不多

属性 作用
android:foreground 设置改帧布局容器的前景图像
android:foregroundGravity 设置前景图像显示的位置
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:foreground="@drawable/test"   // 前景图片
    android:foregroundGravity="right|bottom">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="@color/colorPrimaryDark"/>

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@color/colorAccent"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"/>

</FrameLayout>


三个ImageView设置不同大小与背景色,依次覆盖,接着右下角的是前景图像,通过 android:foreground="@drawable/logo"设置前景图像的图片
android:foregroundGravity="right|bottom"设置前景图像的位置在右下角

三、 LinearLayout(线性布局)

顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的。

1. 排列方式
  • 纵向:android:orientation="vertical"
  • 横向:android:orientation="horizontal"
    系统默认采用横向布局

代码只需要修改android:orientation="horizontal"即可。下面为对比代码和对比图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"> 
    <!--横向布局-->

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"/>

</LinearLayout>
2. 对齐方式

线性布局里有两种设置边距的方式,分别是padding()margin()。前者规定内边距,后者规定外边距。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <!--横向布局-->

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"/>

    <TextView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="@color/colorAccent"
        android:paddingStart="150dp"  //和左侧的边距
        android:paddingTop="50dp"   //和顶部的边距
        android:text="这是一个子控件"

        ></TextView>
    
</LinearLayout>


黑色箭头为margin(),白色箭头为padding(),可以看到文字可以和其背景对齐,这一整个文本控件又和界面对齐。线性布局中,各个控件不能重叠

3. 权重

线性布局中可以规定控件的权重,通过android:layout_weight=""实现。下面我们来看一起权重的经典问题。我们先不设置总权重,设置子元素的宽度为0dp。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">
    <!--横向布局-->


    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"//果绿色
        android:layout_weight="1"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"//玫红色
        android:layout_weight="2"/>

</LinearLayout>

可以看到界面控件的比例与权重相符,再看一下权重超过设定的情况。下面规定总权重为3,子元素宽度都设置为0dp,看看会发生什么?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:weightSum="3"> // 规定总权重为3


    <ImageView
        android:layout_width="0dp" // 设置宽度
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:layout_weight="1"/>

    <ImageView
        android:layout_width="0dp" // 设置宽度
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:layout_weight="2"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:layout_weight="3"/>
    
</LinearLayout>


明明我们设置了1:2:3,为什么显示的却是1:2呢?这是因为系统中规定,子权重不能大于总权重,如果大于了,则“先到先得”。这里我们用了横向排列方式,故第一个控件(果绿色)实际权重为1 * (1+2+3) = 1/6,第二个控件(玫红色)的实际权重为2 * (1+2+3) = 2/6超过总权重的控件将不被显示,那么显示的比例就是(1/6) : (2/6)1 : 2

注意:使用权重时,尽量将控件宽度设置为0dp,否则其比例并不会按其权重显示,那么如何显示呢?这里有一篇文章我觉得讲得挺好的,可以直接看其文字部分。戳我查看

四、 RelativeLayout(相对布局)

在相对布局中,可以方便地设置控件间的间距等。它在MarginLayout的基础上,添加了对齐方法——layout_alignBottom="@+id/iv"。对齐指的是和其他控件对齐。

下面是一些简单的属性:

属性 作用
layout_marginRight 控件与界面右侧距离
layout_toRightOf 将该控件的右边缘与给定ID的控件左边缘对齐;
layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐;
layout_alignParentRight 将该控件的右部与其父控件的右部对齐;
layout_centerInParent 将该控件的置于父控件的中央;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:weightSum="3">

    <View
        android:id="@+id/v1"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:background="@color/colorPrimaryDark" />

    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        android:layout_alignRight="@id/v1"
        android:layout_alignBottom="@id/v1"
        android:layout_marginRight="50dp"/>
    
</RelativeLayout>


通过相对布局,可以实现控件的重叠。当控件大量重叠时,用相对布局更加方便

五、ConstraintLayout(约束布局)

1. 简单介绍
约束布局减少了嵌套,可以使得界面的效率更高,故六大布局方法中,约束布局为人们所推崇。

下面是一些简单的属性:

属性 作用
layout_constraintTop_toTopOf 视图的上边对齐另一个视图的上边
layout_constraintTop_toBottomOf 视图的上边对齐另一个视图的底边
layout_constraintTop_toLeftOf 视图的上边对齐另一个视图的左边
layout_constraintTop_toRightOf 视图的上边对齐另一个视图的右边

同理,左侧、右侧、底部的属性也是类似这样,就不举例了。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:weightSum="3">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_A_toEndOf="parent"
        app:layout_A_toBottomOf="parent"

        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

该例子中,控件四周边距都为20dp。这个还是比较容易实现的,当有两个及以上控件时该怎么办呢?看下面这个例子。

2. 进阶使用
下面我们实现两个控件和父视图边距都为20dp,之间的距离也为20dp

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:weightSum="3">

    <View
        android:id="@+id/v1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/v2"


        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"

        app:layout_constraintHorizontal_weight="1"/>

    <View
        android:id="@+id/v2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"

        app:layout_constraintTop_toTopOf="@id/v1"
        app:layout_constraintBottom_toBottomOf="@id/v1"

        app:layout_constraintStart_toEndOf="@id/v1"
        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_weight="1"
        android:layout_marginRight="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout>


相信你在这个例子中,可以很好的感受到layout_constraintStart_toStartOflayout_constraintEnd_toStartOf的区别。这里还使用了权重,权重的好处在于,当你确定各个控件的比例时,不用通过繁琐的计算各个边距。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,873评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,483评论 1 306
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,525评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,595评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,018评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,958评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,118评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,873评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,643评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,813评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,293评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,615评论 3 262
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,306评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,170评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,968评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,107评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,894评论 2 278

推荐阅读更多精彩内容