共计 4953 个字符,预计需要花费 13 分钟才能阅读完成。
Android 有几种布局?
- LinearLayout(线性布局)
- RelativeLayout(绝对布局)
- FrameLayout(帧布局)
- TableLayout(表格布局)
- GridLayout(网格布局)
- AbsoluteLayout(相对布局)
LinearLayout
LinearLayout 又称作线性布局,是一种十分罕用的布局。
LinearLayout 外面能够搁置多个 view(这里称为子 view,子项)。子 view 能够是 TextView,Button,或者是 LinearLayout,RelativeLayout 等等。它们将会按程序顺次排布为一列或一行。接下来介绍一些在 xml 中的设置。
先在 xml 中放一个 LinearLayout。
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
</LinearLayout> |
上面介绍一些属性。
竖直排布与程度排布
通过设置 orientation 来确定程度或竖直排布子 view。可选值有 vertical 和 horizontal。
竖直排布
设置 orientation 为 vertical。
android:orientation="vertical"
程度排布
设置 orientation 为 horizontal。
android:orientation="horizontal"
排布形式 gravity
决定子 view 的排布形式。gravity
有“重力”的意思,咱们引申为子 view 会向哪个方向聚拢。gravity
有几个选项能够抉择,咱们罕用的有 start,end,left,right,top,bottom。
例如:
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="start" | |
android:orientation="vertical"> | |
</LinearLayout> |
上面是 gravity 的选项。咱们会把 LinearLayout 叫做“父 view”或者“容器”。
常量 | 定义值 | 形容 |
---|---|---|
top | 30 | 把 view 推到容器里的顶部。不会扭转 view 的尺寸。 |
bottom | 50 | 把 view 推到容器的底部。不会扭转 view 的尺寸。 |
center | 11 | 子 view 程度与竖直都居中。不会扭转 view 的尺寸。 |
center_horizontal | 1 | 子 view 程度居中。不会扭转 view 的尺寸。 |
center_vertical | 10 | 子 view 垂直居中。不会扭转 view 的尺寸。 |
start | 800003 | 把 view 推到容器里最开始的中央。不会扭转 view 的尺寸。 |
end | 800005 | 把子 view 放到容器的最尾部。不扭转 view 的尺寸。 |
left | 3 | 子 view 从容器的右边开始排布。不会扭转 view 的尺寸。 |
right | 5 | 子 view 从容器的左边开始排布。不会扭转 view 的尺寸。 |
start 和 left,end 和 right 并不一定是同样的成果。对于 RTL(right to left)类型的手机,比方某些阿拉伯文的零碎。start 是从右到左的。咱们日常生活中很少见到 RTL,个别都是 LTR。但还是倡议多用 start 而不是 left。
gravity 能够同时设置多个值,用或符号 |
来连贯。比方android:gravity="end|center_vertical"
。
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="60dp" | |
android:gravity="end|center_vertical" | |
android:orientation="vertical"> | |
</LinearLayout> |
子 view 的 layout_gravity
layout_gravity 看起来和 gravity 有些类似。
android:gravity
管制本人外部的子元素。android:layout_gravity
是通知父元素本人的地位。
取值范畴和 gravity 是一样的。代表的含意也类似。
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="100dp" | |
android:orientation="horizontal"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="#90CAF9" | |
android:text="none" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:background="#9FA8DA" | |
android:text="center" /> | |
</LinearLayout> |
宰割占比 layout_weight
能够在设置子 view 的 layout_weight,来确定空间占比。设置 layout_weight
的时候,个别要设置 layout_width="0dp"
。
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="10dp" | |
android:background="#FFCC80" | |
android:orientation="horizontal"> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:background="#eaeaea" | |
android:gravity="center" | |
android:text="1" /> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="2" | |
android:text="2" /> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:background="#eaeaea" | |
android:text="1" /> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="3" | |
android:background="#BEC0D1" | |
android:text="3" /> | |
</LinearLayout> |
宰割占比之和 weightSum
android:weightSum
定义子 view 的 weight 之和的最大值。如果不间接指定,它会是所有子 view 的 layout_weight
之和。如果想给独自的一个子 view 一半的空间占比,能够设置子 view 的 layout_weight 为 0.5,并且设置 LinearLayout 的 weightSum 为 1.0。
取值能够是浮点数,比方9.3
。
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="10dp" | |
android:background="#4DB6AC" | |
android:weightSum="9.3" | |
android:orientation="horizontal"> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="4.6" | |
android:background="#eaeaea" | |
android:gravity="center" | |
android:text="4.6" /> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:background="#7986CB" | |
android:layout_weight="2.5" | |
android:text="2.5" /> | |
</LinearLayout> |
分割线 divider
设置 divider 与 showDividers 属性。
<pre id="__code_8" style="box-sizing: inherit; color: var(--md-code-fg-color); font-feature-settings: "kern"; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; direction: ltr; position: relative; margin: 1em 0px; line-height: 1.4;"> `android:divider="@drawable/divider_linear_1" | |
android:showDividers="middle" |
间接给 divider 设置色彩是有效的。
在 drawable 目录里新建一个 xml,叫做divider_linear_1.xml
。
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | |
<solid android:color="#7986CB" /> | |
<size | |
android:width="1dp" | |
android:height="1dp" /> | |
</shape> |
size 必须指定,否则当做 divider 来用时会显示不进去。
LinearLayout 设置 divider。
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#eaeaea" | |
android:divider="@drawable/divider_linear_1" | |
android:orientation="vertical" | |
android:showDividers="middle"> | |
.... |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="100dp" | |
android:layout_marginTop="20dp" | |
android:background="#FFD9D9" | |
android:divider="@drawable/divider_linear_1" | |
android:orientation="horizontal" | |
android:showDividers="middle"> | |
... |
showDividers 有几种可选:
- middle 两头的分割线
- beginning 开始的分割线
- end 完结的分割线
- none 没有分割线
LinearLayout 线性布局入门视频参考