1. LineraLayout局部 线性布局
感觉这个布局方式是最常见的一种。它有两种布局方式:行 与列。是由参数orientation(方位)决定的。vertical表示竖直排列,horizontal表示横向排列.
具体代码如
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
每个参数都要以android:开头。一般都有android:layout_width和android:layout_height 具体的值有两个一个是fill_parent 一个是wrap_content 每一个布局都是一个容器,layout_width与height表示这个容器的宽 与高是填充满父容器,还是依赖于他本身的内容
2. FrameLayout布局 框架布局
这个布局一般是放图片的,放入其中的所有元素都被放置在FrameLayout区域最左上的区域,而且无法为这些元素指定一个确切的位置。如果一个FrameLayout里有多个子元素,那么后边的子元素的显示会重叠在前一个元素上。这个布局方式的宽与高的参数一般是用wrap_content。
3. RelativeLayout布局 相对布局
这个布局很常见。具体代码比如:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/blue" android:padding="10dip">
<TextView android:id="@+id/label" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="请输入用户名:" />
<!--
这个EditText放置在上边id为label的TextView的下边
-->
<EditText android:id="@+id/entry" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label" />
<!--
取消按钮和容器的右边齐平,并且设置左边的边距为10dip
-->
<Button android:id="@+id/cancel" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip" android:text="取消" />
<!--
确定按钮在取消按钮的左侧,并且和取消按钮的高度齐平
-->
<Button android:id="@+id/ok" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/cancel"
android:layout_alignTop="@id/cancel" android:text="确定" />
</RelativeLayout>
在这个容器里放置了一些组件,每个组件里多了一些对他们位置关系的描述,比如android:layout_below="@id/label" 表示放在id为label的控件下面== 要注意的是,如果组件B依赖于A,那么必须要让A出现在B的前面。
padding表示填充,margin表示边距。android当中最常见的支持描述大小区域的类型如下 px像素 dip依赖于设备的像素.
4. TableLayout 表格布局
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="用户名:" android:textStyle="bold"
android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/username" android:padding="3dip"
android:scrollHorizontally="true" />
</TableRow>
<TableRow>
<TextView android:text="登录密码:" android:textStyle="bold"
android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/password" android:password="true"
android:padding="3dip" android:scrollHorizontally="true" />
</TableRow>
<TableRow android:gravity="right">
<Button android:id="@+id/cancel"
android:text="取消" />
<Button android:id="@+id/login"
android:text="登录" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
里面是由多个TableRow组成的。一行一行的很好理解。
布局方式是可以组合在一起的。这个也不难理解。