Android的布局的布局类型不多,只有五种。以下是 Android的五种布局,我们可以做一些了解。
1.LinearLayout(线性布局)
线性布局可以设置为水平和垂直两种格式。只能进行单行布局。在布局的使用里线性布局可以算是用得比较多的一种布局了。
1)水平线性布局的话,在一行的位置不足放下组件时会自动换行,位置不足的那个组件自动跳到下一行。
2)垂直线性布局则是不管组件的宽度如何,每个组件就占用一行。
layout_width用于设置布局的宽度
layout_height用于设置布局的高度
orientation用于设置布局是水平(horizontal)还是垂直(vertical)
下面是关于线性布局使用的一个例子:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff000000"
android:text="@string/user_name"/>
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_password"
/>
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_login"
android:onClick="second"
/>
</LinearLayout>
2.FrameLayout(帧布局/框架布局)
框架布局是五大布局里最简单的一个布局,整个界面被当成一块空白备用区域以填充所有组件,组件的位置都不能被指定,都会被放置在区域的左上角且层叠进行显示,后面添加的组件会叠在前面的组件之上将其覆盖。框架布局使用得比较少。
下面是关于框架布局使用的一个例子:
由于两个TextView大小一样,所以第二个TextView将第一个添加的TextView给全部覆盖掉,只显示一个也就是第二个TextView。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myproject.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff000000"
android:text="用户名"/>
<EditText
android:id="@+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
/>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="second"
android:text="登陆" />
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</FrameLayout>
3.RelativeLayout(相对布局)
相对布局按各组件之间位置关系完成的布局。组件通过与位置相关的一些属性(例如layout_below和layout_above……)与其各自id配合指定位置关系(id必须在定义之后才可以被引用)。
该布局管理器会根据最近的一个视图组件或顶层父组件来确定下一个组件的位置。改变一个组件的位置其他的组件位置也会随之改变,相当于移动的一个整体组件。
4.TableLayout(表格布局)
表格布局适用于n行n列的布局格式,一个TableLayout由许多的TableRow组成,一个TableRow就代表了TableLayout中的一行,可向行中增加组件。
与其他布局的属性值设置限制不同的是,TableRow的oriention属性值恒为horizontal,且layout_width和layout_height属性值恒为match_parent和wrap_content。组件都是横行排列且高宽一致。
TableRow单元格可以为空,但不能跨列。表格布局也是使用的比较多的一种布局。
5.AbsoluteLayout(绝对布局)
该布局利用layout_x和layout_y两个属性来指定组件的位置,描述组件的坐标位置。(x,y)左边是横行方向的位置,y是纵向方向的位置。x越往右其值越大,y则越往下值越大。
绝对布局虽然让程序员随意精确操作组件位置,但由于局限性比较大,不利于程序的扩展性和兼容性,通常在项目开发中不会使用该布局。
至于后面三种布局的列子,我就不再列举了,布局算是比较简单的部分,大概和前面布局使用差不太多。只是注意各自属性的使用和值大小的定义。
前面说的都是单一布局的使用以及功能介绍,其实在实际应用里我们使用的常常不只是使用一种布局,我们比较习惯的都是布局与布局的嵌套使用。