在android 的开发过程当中,虽然平台提供了很多的布局方式,以现在的互联网开发app,用到的最多的就是linearLayout,简单,快速能做出布局。实在有实现不了的,就用一下相对布局。
其它几种布局方式,很少有机会用到。
下面是闲时无聊写的几个示例。 希望可以帮助到有需要的人。
代码没有完美一说,别人看你代码像一坨,很正常。
1、网格布局
截图:
代码:
<GridLayout
android:layout_width="match_parent"
android:columnCount="3"
android:rowCount="3"
android:layout_height="wrap_content">
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:background="#FF6F00" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:background="#FF6F00" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:background="#FF6F00" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:layout_gravity="fill"
android:layout_columnSpan="2"
android:background="#FF6F00" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:background="#FF6F00" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:background="#FF6F00" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_columnWeight="1"
android:layout_margin="3dp"
android:background="#FF6F00" />
</GridLayout>
2、表格布局
截图:
代码:
<TableLayout
android:layout_width="match_parent"
android:layout_margin="3dp"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="3"
android:layout_margin="3dp"
android:background="#ffdddd" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="7"
android:layout_margin="3dp"
android:background="#6997BC" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="7"
android:layout_margin="3dp"
android:background="#74C67A" />
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="3"
android:layout_margin="3dp"
android:background="#C5BC76" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:layout_margin="3dp"
android:background="#FF6F00" />
</TableRow>
</TableLayout>
3、线性布局
截图:
代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@color/blue"
android:gravity="center|center_horizontal|center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@color/orange"
android:gravity="center|center_horizontal|center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮二" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@color/normal"
android:gravity="center|center_horizontal|center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮三" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="5dp"
android:layout_weight="2"
android:background="@color/orange"
android:gravity="center|center_horizontal|center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮四" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="@color/teal_700"
android:gravity="center|center_horizontal|center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮五" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@color/warning"
android:gravity="center|center_horizontal|center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮六" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
4、相对布局:
截图:
代码:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:gravity="center|center_vertical|center_horizontal"
tools:context=".test.SqliteActivity">
<RelativeLayout
android:layout_width="900dp"
android:layout_margin="5dp"
android:padding="10dp"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/btn2"
android:background="#736C67"
android:layout_marginRight="5dp"
android:text="按钮1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_toLeftOf="@+id/btn3"
android:background="#BA5F19"
android:layout_marginRight="5dp"
android:text="按钮2" />
<Button
android:id="@+id/btn3"
android:layout_width="300dp"
android:layout_height="103dp"
android:layout_alignParentEnd="true"
android:background="#60974B"
android:text="按钮3" />
<Button
android:id="@+id/btn4"
android:layout_width="575dp"
android:layout_height="50dp"
android:layout_below="@+id/btn1"
android:layout_alignStart="@+id/btn1"
android:background="#548EBC"
android:layout_marginTop="5dp"
android:text="按钮4" />
</RelativeLayout>
</LinearLayout>