Android布局
Android布局
1.1 属性--orientation
LinearLayout是布局中最基本的控件,也是最常用的布局控件。它有两种,一种是水平设置(horizontal);另一种是垂直设置(vertical),通过属性orientation来设置,水平布局时只有一行可以有若干列;垂直布局时只有一列,可以有若干行。
1.2属性--gravity
android:gravity属性用来控制放置在本LinearLayout内的控件的对齐方式(控制所有子控件在父容器中的位置)
android:layout_gravity属性是本LinearLayout控件相对于父容器的对齐方式
1.3属性--weightandroid:layout_weight它是一个比较重要的属性,它的意思就是“权重”。weight可以分配在父容器中的空间比例
1.4 LinearLayout可以嵌套,也可以放置其他布局控件。代码如下:
<?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:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="左上按钮"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="左下按钮"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="中间按钮"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上按钮"
android:layout_gravity="top|right"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="右下按钮"
android:layout_gravity="bottom|right"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
案列如下:
2.表格布局控件TableLayout
一个TableLayout里面有表格行控件TableRow,每个TableRow里布置各自的控件,好比是一个大架子里面放了很多个抽屉,各个抽屉里放着各自的东西,类型可以不一样,大小可以不一样。如果不用TableRow控件的话就会默认占一行。表格布局有一个局限性就是它只跨列不跨行,但是可以对列进行伸(stretchColumns)缩(shrinkColumns)。它还可以对控件进行隐藏(collapse.....)--------表格布局一般来说比其它布局少用
3.网格布局控件GridLayout
GridLayout布局不仅可以跨列(columsCount)还可以跨行(rowCount),它就没有了TableLayout的局限--只能跨列。
4.框架布局控件 /桢布局控件FrameLayout
它是所有布局中最容易理解的,一个比较简单的布局控件。里面的控件关系为:后面控件会覆盖前面的控件。
5.下面为布局控件的基本概念