这是我参与 8 月更文挑战的第 26 天,活动详情查看: 8月更文挑战
简介
LinearLayout就是线性布局. 从字面上理解, 就是当前布局仅支持其子布局按照线的形式进行排列,LinearLayout支持两种排列, 及垂直排列和水平排列.
排列方式(orientation)
LinearLayout支持两种排列:垂直排列和水平排列.默认布局为水平排列,如果不指定排列方式, 当布局超过屏幕宽度时, AS工具会标红
<LinearLayout 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"
tools:context=".LinearLayoutActivity">
<Button
android:text="第一个按钮"
android:layout_marginEnd="20dp"
android:layout_width="200dp"
android:layout_height="50dp"/>
<Button
android:text="第二个按钮"
android:layout_width="match_parent"
android:layout_height="50dp"/>
</LinearLayout>
当orientation
指定为vertical
时
权重layout_weight
注意如果指定权重时是根据
orientation
的排列方式来展示的, 对应的宽高要设置为0,例如:如果要指定控件的宽度的权重,就需要把layout_width设置为0dp
水平权重
垂直权重
weightSum 权重数量
LinearLayout可以指定当前布局内的权重总和,例如,指定当前布局分为六等分, 两个控件各占一份, 其余的留白
如果控件内部权重总和超过父布局指定权重数量, 则最多显示父布局的总和的权重,如果父布局没有指定权重,则控件内部的权重总和为父布局的weightSum权重数量
基本属性
android:layout_width
控件宽度,可以设置为wrap_content
自适应,match_parent
填充父窗体以及自定义宽度
android:layout_height
控件高度,可以设置为wrap_content
自适应,match_parent
填充父窗体以及自定义高度
android:orientation
可选属性有两个,水平排列horizontal
(默认)和垂直排列vertical
android:weightSum
权重标记
android:layout_weight
指定当前控件的权重
android:layout_gravity
相对父布局的位置
android:gravity
指定子布局的位置,可选有top
,bottom
,left
,right
,center_vertical
,fill_vertical
,center_horizontal
,fill_horizontal
,center
,fill
,clip_vertical
,clip_horizontal
,start
,end