我先把整个窗口 进行了垂直对齐,把窗口分成了上中下三层,用到的是一个权重属性:android:layout_weight="1",就是按比例分配大小,上下设置为1,中间层设置8,然后再在每一层里面,对按钮进行布局,中间层里面还要嵌套一个线性布局,这是因为android:orientation="horizontal",这个布局方向的特点,默认是水平对齐,如果设置成水平对齐了,那么里面的元素就不能进行水平方向的修改(android:layout_gravity="center_horizontal"就没有效果),相反同理,所以就要用两层线性布局嵌套来解决。
下面是activity_main.xml
<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"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="左上" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="右上" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="中间" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="左下"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:text="2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="右下"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>