线性布局
排列方式
android:orientation="vertical" 垂直排列
android:orientation="horizontal" 水平排列(不写时默认为此)
对齐
android:gravity="center|bottom" 组件所包含的子元素的对齐方式,可多个组合
android:layout_gravity="center_horizontal" 控制该组件在父容器里的对齐方式
<LinearLayout android:background="@color/teal_700" android:layout_gravity="center_horizontal" android:layout_width="100dp" android:layout_height="100dp"/> <LinearLayout android:background="@color/purple_200" android:layout_width="100dp" android:layout_height="100dp"/> <LinearLayout android:background="@color/teal_200" android:layout_width="100dp" android:layout_height="100dp"/>
分割线
android:divider="@drawable/mouse" 设置分割线
android:showDividers="middle" 设置分割线所在位置(none无、beginning开始、end结束、middle每两个组件之间)
android:dividerPadding="15dp" 设置分割线的padding,只有左右
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:divider="@drawable/mouse" android:showDividers="middle" android:dividerPadding="15dp"> <TextView android:id="@+id/tv" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:background="@color/purple_200" android:layout_width="100dp" android:layout_height="100dp"/> <LinearLayout android:background="@color/purple_500" android:layout_width="100dp" android:layout_height="100dp"/> <LinearLayout android:background="@color/purple_700" android:layout_width="100dp" android:layout_height="100dp"/> </LinearLayout>
分割线另一种做法
<LinearLayout android:layout_weight="1" android:background="@color/teal_700" android:layout_gravity="center_horizontal" android:layout_width="100dp" android:layout_height="0dp"/> <View android:layout_width="match_parent" android:layout_height="5dp" android:background="@color/red"/> <LinearLayout android:layout_weight="2" android:background="@color/purple_200" android:layout_width="100dp" android:layout_height="0dp"/> <LinearLayout android:background="@color/teal_200" android:layout_width="100dp" android:layout_height="0dp"/>
权重
android:layout_weight="1" 该属性是用来等比例的划分区域,分配的是剩余空间,组件高度、宽度为match_parent时会有问题