Android回顾之布局

前面回顾了四大组件,今天我们来回顾一下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"
>

<LinearLayout
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:orientation="horizontal" >

	<EditText
		android:id="@+id/url"
		android:layout_width="200dp"
		android:layout_height="match_parent"
		android:layout_weight="0.77"
		android:text="请输入网址" />

	<ImageButton
		android: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">

	<Button
		android:id="@+id/previous_music"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignParentLeft="true"
		android:background="@drawable/previous_music_selector" />

	<Button
		android: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" />

	<Button
		android: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" />

	<Button
		android: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" />

	<Button
		android: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>


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值