1.线性布局在水平方向居中排列并靠近底部
在activity_main.xml文件中加入如下代码行
android:gravity="center_horizontal|bottom"
2.子线性布局水平居中排列
红色方格水平居中排列
<LinearLayout
android:layout_gravity="center_horizontal"
android:background="#ff0000"
android:layout_width="100dp"
android:layout_height="100dp"/>
3.用图片设置组件之间的分割线
两行代码一起作用
android:divider="@drawable/divider"
android:showDividers="beginning"
分割线与左右两边间距(布局与分割线一起移动)
android:padding="100dp"
分割线与左右两边间距(只有分割线移动)
4.等比例分配权重
例如设置绿色的权重为1,分配的是剩余空间(保留下红色和蓝色剩下的最大的空间)
android:layout_weight="1"
同时设置权重
<LinearLayout
android:layout_weight="1"
android:background="#ff0000"
android:layout_width="100dp"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_weight="1"
android:background="#00ff00"
android:layout_width="100dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_weight="1"
android:background="#0000ff"
android:layout_width="100dp"
android:layout_height="match_parent"/>
权重计算方法:例如三个布局的高全部设置为match_parent,去掉分割线
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:showDividers="middle"
android:dividerPadding="100dp"
android:orientation="vertical">
<LinearLayout
android:layout_weight="2"
android:background="#ff0000"
android:layout_width="100dp"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_weight="1"
android:background="#00ff00"
android:layout_width="100dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_weight="1"
android:background="#0000ff"
android:layout_width="100dp"
android:layout_height="match_parent"/>
</LinearLayout>
此时,权重大的红色布局反而不见了,剩下的等分
计算方式:有一个布局中红绿蓝三个布局分,1-3=-2
红色占比为权重的四分之二,权重的计算方式为1+剩余空间,
则1-2*(2/4)=0
所以一般设置高为0dp
<LinearLayout
android:layout_weight="2"
android:background="#ff0000"
android:layout_width="100dp"
android:layout_height="0dp"/>
<LinearLayout
android:layout_weight="1"
android:background="#00ff00"
android:layout_width="100dp"
android:layout_height="0dp" />
<LinearLayout
android:layout_weight="1"
android:background="#0000ff"
android:layout_width="100dp"
android:layout_height="0dp"/>
5.用View设置组件之间的分割线
<View
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="1dp"/>
效果图