5.1LinearLayout-线性布局:
LinearLayout-线性布局有两个方向:水平和垂直方向。分别是通过android:orientation="horizontal"和android:orientation="vertical"来控制的。
权重,也就是对控件设置 android:layout_weight的属性。这个属性的意思是分配剩余空间。
比如有俩个控件,分别设置为android:layout_weight=“1”,android:layout_weight=“2”,表示控件分别占屏幕的1/3和2/3,。不过这是有一个前提的,就是建立在控件的宽度或者高度设置为0dp的情况下。
5.2RelativeLayout(相对布局)
相对,顾名思义是有参照的,就是以某个兄弟组件,或者父容器来决定的(兄弟组件是在一个同一个布局里面的组件,如果是布局里一个组件参照另一个布局里的组件会出错)
android:paddingBottom:设置控件内容与控件下边缘的距离
android:paddingTop:设置控件内容与控件上边缘的距离
android:paddingLeft:设置控件内容与控件左边缘的距离
android:paddingRight:设置控件内容与控件右边缘的距离
android:layout_marginBottom:设置此控件的下边缘与其他控件的距离
android:layout_marginTop:设置此控件的上边缘与其他控件的距离
android:layout_marginLeft:设置此控件的左边缘与其他控件的距离
android:layout_marginRight:设置此控件的右边缘与其他控件的距离
5.3FrameLayout(帧布局)
默认是按照左上角(0,0)开始排布,在帧布局空定义的控件每一个都是以画面的形式进行呈现。最开始定义的控件出现在最下方哪个,最后定义的控件出现的最上面。
帧布局可以使用在手机联系人的导航显示上字母的呈现。帧布局使用在帧动画。
例:
代码如下:
<FrameLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id="@+id/FrameLayout1"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity"
android:foreground="@drawable/logo"
android:foregroundGravity=“right|bottom”>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FF6143" />
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#7BFE00" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFFF00" /> </FrameLayout>
5.4TableLayout(表格布局)
继承自LinearLayout
stretchColumns:拉伸某一列。让布局显得不紧凑。
shrinkColumns:回缩某一列,让整体的内容都得以呈现。
collapseColumns:隐藏某一列
TableRow的宽和高可以不指定,系统会自动给定对应的宽和高。
例:
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TableRow >
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
></Button>
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
></Button>
<Button
android:id="@+id/button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"
></Button>
</TableRow>
<TableRow >
<Button
android:id="@+id/button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4"
></Button>
<Button
android:id="@+id/button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5"
></Button>
<Button
android:id="@+id/button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮6"
></Button>
</TableRow>
<TableRow >
<Button
android:id="@+id/button07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮7"
></Button>
<Button
android:id="@+id/button08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮8"
></Button>
<Button
android:id="@+id/button09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮9"
></Button>
</TableRow> </TableLayout>
5.5绝对布局(AbsoluteLayout)
根据绝对的像素点位置进行排布,默认以左上角为起点。
使用的时候一般会通过Layout_x和Layout_y来制定对应的控件存放的位置,不利于屏幕适配。
例:
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent” android:layout_height=“match_parent”>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="236dp"
android:layout_y="94dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="143dp"
android:layout_y="166dp"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="263dp"
android:text="Button" /> </AbsoluteLayout>
5.6GridLayout(网格布局)
在4.0之后出现的布局。
columnCount:存在多少列
RowCount:存在多少行
layout_rowSpan:占据多少行
layout_columnSpan:占据多少列
GridLayout和TableLayout有什么不同?
TableLayout定义TableRow来呈现内容。
GridLayout中可以定义控件来直接使用。
表格布局中只能合并列不能合并行。
但是网格布局中既能合并列也能合并行。
例:
代码如下:
<GridLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id="@+id/GridLayout1"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:rowCount=“6”
android:columnCount=“4”
android:orientation=“horizontal”>
<TextView
android:layout_columnSpan="4"
android:text="0"
android:textSize="50sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
<Button
android:text="回退"
android:layout_columnSpan="2"
android:layout_gravity="fill"
/>
<Button
android:text="清空"
android:layout_columnSpan="2"
android:layout_gravity="fill"
/>
<Button
android:text="+"
/>
<Button
android:text="1"
/>
<Button
android:text="2"
/>
<Button
android:text="3"
/>
<Button
android:text="-"
/>
<Button
android:text="4"
/>
<Button
android:text="5"
/>
<Button
android:text="6"
/>
<Button
android:text="*"
/>
<Button
android:text="7"
/>
<Button
android:text="8"
/>
<Button
android:text="9"
/>
<Button
android:text="/"
/>
<Button
android:layout_width="wrap_content"
android:text="."
/>
<Button
android:text="0"
/>
<Button
android:text="="
/> </GridLayout>