Android回顾之布局

来源:互联网 发布:逆战刷nz点软件 编辑:程序博客网 时间:2024/05/01 14:57

前面回顾了四大组件,今天我们来回顾一下Android的布局方式

一、LinearLayout

LinearLayout可以说是最常用到的布局方式了。LinearLayout是按照水平或是垂直的方式排列元素,垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。当然,我们也可以吧这两种方式混合在一起使用。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><EditTextandroid:id="@+id/url"android:layout_width="200dp"android:layout_height="match_parent"android:layout_weight="0.77"android:text="请输入网址" /><ImageButtonandroid:id="@+id/go"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_btn_search" /></LinearLayout> <WebView android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/webview"></WebView>   </LinearLayout>




二、AbsoluteLayout

AbsoluteLayout是绝对布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。这种布局方式由于位置写死,对于屏幕大小的适应性不好,不利于适配,在开发中不推荐使用。

三、RelativeLayout

与Absolutelayout对应,有RelativeLayout相对布局,RelativeLayout按照各子元素之间的位置关系完成布局。子元素通过元素ID来进行相对位置的布局。
RelativeLayout里常用的位置属性如下:
android:layout_toLeftOf —— 该组件位于引用组件的左方android:layout_toRightOf —— 该组件位于引用组件的右方android:layout_above —— 该组件位于引用组件的上方android:layout_below —— 该组件位于引用组件的下方android:layout_alignParentLeft —— 该组件是否对齐父组件的左端android:layout_alignParentRight —— 该组件是否齐其父组件的右端android:layout_alignParentTop —— 该组件是否对齐父组件的顶部android:layout_alignParentBottom —— 该组件是否对齐父组件的底部android:layout_centerInParent —— 该组件是否相对于父组件居中android:layout_centerHorizontal —— 该组件是否横向居中android:layout_centerVertical —— 该组件是否垂直居中
RelativeLayou布局相对来说使用比较灵活,适合使用在相对较复杂的布局中,这里给大家一个例子
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/homeRLLayout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/main_bg03"    android:orientation="vertical"><Buttonandroid:id="@+id/previous_music"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:background="@drawable/previous_music_selector" /><Buttonandroid:id="@+id/repeat_music"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/previous_music"android:background="@drawable/repeat_none_selector" /><Buttonandroid:id="@+id/play_music"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/repeat_music"android:background="@drawable/pause_selector" /><Buttonandroid:id="@+id/shuffle_music"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/play_music"android:background="@drawable/shuffle_none_selector" /><Buttonandroid:id="@+id/next_music"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:background="@drawable/next_music_selector" /></RelativeLayout>



四、FrameLayout

FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。这个布局方式简单,实用价值不大。

五、TableLayout

TableLayout顾名思义,此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。TableRow中可以定义自己的元素。TableLayout的使用和HTML中的Table相差不大。
这种布局方式很适合用来布局一些类似表格的布局,如:
<?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>        <TextView            android:text="用户名:"/>        <EditText             android:id = "@+id/userName"           android:layout_height="wrap_content"           android:layout_width="200dp"/>            </TableRow><TableRow>        <TextView            android:text="密码:"/>        <EditText             android:id = "@+id/passWord"            android:layout_height="wrap_content"            android:layout_width="200dp"    />            </TableRow></TableLayout>


以上几种是比较常见的布局方式,他们单独使用或许很普通,但是如果配合好的话,能够完成很不错的布局。



0 0