1.LinearLayout(线性布局)
有两种形式:水平线性布局--horizontal,垂直线性布局--vertical
具体效果如下:
一个总的LinearLayout包含两个小的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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="horizontal">
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#b2dfdb" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#80cbc4" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#4db6ac" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#26a69a" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#b2dfdb" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#80cbc4" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#4db6ac" />
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#26a69a" />
</LinearLayout>
</LinearLayout>
2.RelativeLayout(相对布局)
相对布局通常也有两种形式,一种是相对容器而言,一种是相对控件而言的
有三种类型的属性:
属性值是true或false
android:layout_centerHrizontal水平居中
android:layout_centerVertical垂直居中
android:layout_centerInparent相对于父元素完全居中。
android:layout_alignParentBottom位于父元素的下边缘
android:layout_alignParentTop位于父元素的上边缘
android:layout_alignParentLeft位于父元素的左边缘
android:layout_alignParentRight位于父元素的右边缘
属性值是”@id/*“
android:layout_below在某元素的下方
android:layout_above在某元素的上方
andorid:layout_toRightOf在某元素的右方
android:layout_toLeftOf在某元素的左方
android:layout_alignBottom和某元素下方对齐
android:layout_alignTop和某元素上方对齐
android:layout_alignRight和某元素右方对齐
android:layout_alignLeft和某元素左方对齐
属性值是数值
android:layout_marginLeft离某元素左边缘的距离
android:layout_marginRight离某元素右边缘的距离
android:layout_marginTop离某元素上边缘的距离
android:layout_marginBottom离某元素下边缘的距离
具体代码如下:
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textview1"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="#b2dfdb" />
<TextView
android:id="@+id/textview2"
android:layout_width="96dp"
android:layout_height="96dp"
android:background="#26a69a"
android:layout_below="@+id/textview1"
android:layout_toRightOf="@+id/textview1"
android:layout_toEndOf="@+id/textview1" />
<TextView
android:id="@+id/textview3"
android:layout_width="96dp"
android:layout_height="96dp"
android:background="#80cbc4"
android:layout_below="@+id/textview2"
android:layout_toRightOf="@+id/textview2"
android:layout_toEndOf="@+id/textview2" />
</RelativeLayout>
3.FramLayout(帧布局)
采用帧布局设计界面时,只能在屏幕左上角显示一个控件,如果添加多个控件,这些控件会按照顺序在屏幕的左上角重叠显示,且会透明显示之前控制的文本
具体实现代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="294dp"
android:layout_height="294dp"
android:background="#80cbc4"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="218dp"
android:layout_height="214dp"
android:background="#26a69a"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="156dp"
android:layout_height="143dp"
android:background="#b2dfdb"
android:text="Button3" />
</FrameLayout>
4.GridLayout(网格布局)
实现控件的交错显示,能够避免因布局嵌套对设备性能的影响,利于自由布局的开发。网格布局用一组无限细的直线将区域分成行、列和单元,并指定控件的显示区域和控件在该区域的显示方式
实现代码如下:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="4"
android:orientation="horizontal" >
<Button
android:layout_column="3"
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:layout_gravity="fill"
android:layout_rowSpan="3"
android:text="+" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="0" />
<Button android:text="00" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="=" />
</GridLayout>