现在的位置: 首页 > 综合 > 正文

11_Android布局之线性布局(LinearLayout)和框架布局(FrameLayout)

2013年03月19日 ⁄ 综合 ⁄ 共 6660字 ⁄ 字号 评论关闭

Android布局

1. 线性布局LinearLayout

线性布局是最常用的布局,线性布局在xml文件中用<LinearLayout>来定义;线性布局可以分为水平和垂直方向的布局,可以通过android:orientation=”vertical”来定义方向,广该属性可以有horizontal和vertical两个方向;<LinearLayout>标签中有一个很重要的属性gravity,该属性用于控制布局中视图的位置,如果设置多个值需要使用 | 来进行分隔。该布局属性如下:

(1)android:layout_width和android:layout_height属性说明:

属性

描述

wrap_content

填满父控件的空白

fill_parent

表示大小刚好足够显示当前控件里的内容Android中的fill_parent和match_parent是一样的

match_parent

在Android2.2中开始用match_parent了,较少用fill_parent了

(2)android:layout_weight权重的描述

Layout_weight用于给一个线性布局中的诸多视图的重要度赋值:所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空间,若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight,值以及该值在当前屏幕布局的整体layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。

(3)下面是案例(linear.xml)

界面及结构分析如下图:

linear.xml源码如下:

<?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"
	android:background="#00ff00">
	<!-- 布局是可以嵌套的 -->
	<LinearLayout android:orientation="horizontal" 
		android:layout_width="match_parent" android:layout_height="wrap_content">
		<EditText android:id="@+id/msg" android:layout_width="match_parent"
			android:layout_height="wrap_content"></EditText>
	</LinearLayout>
	<LinearLayout android:orientation="horizontal" 
		android:layout_width="match_parent" android:layout_height="wrap_content">
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="mc"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="m+"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="m-"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="mr"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="horizontal" 
		android:layout_width="match_parent" android:layout_height="wrap_content">
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="C"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="+/-"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="*"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="/"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="horizontal" 
		android:layout_width="match_parent" android:layout_height="wrap_content">
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="7"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="8"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="9"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="-"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="horizontal" 
		android:layout_width="match_parent" android:layout_height="wrap_content">
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="4"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="5"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="6"></Button>
		<Button android:layout_width="match_parent"
			android:layout_weight="1"
			android:layout_height="wrap_content" android:text="+"></Button>
	</LinearLayout>
	<LinearLayout android:orientation="horizontal" 
		android:layout_width="match_parent" android:layout_height="wrap_content">
		<LinearLayout
			android:layout_weight="3" android:orientation="vertical"
			android:layout_width="wrap_content" android:layout_height="wrap_content">
			<LinearLayout android:orientation="horizontal" 
				android:layout_width="match_parent" android:layout_height="wrap_content">
				<Button android:layout_width="match_parent"
					android:layout_weight="1"
					android:layout_height="wrap_content" android:text="1"></Button>
				<Button android:layout_width="match_parent"
					android:layout_weight="1"
					android:layout_height="wrap_content" android:text="2"></Button>
				<Button android:layout_width="match_parent"
					android:layout_weight="1"
					android:layout_height="wrap_content" android:text="3"></Button>
			</LinearLayout>
			<LinearLayout android:orientation="horizontal" 
				android:layout_width="match_parent" android:layout_height="wrap_content">
				<Button android:layout_width="0px"
					android:layout_weight="2"
					android:layout_height="wrap_content" android:text="0"></Button>
				<Button android:layout_width="0px"
					android:layout_weight="1"
					android:layout_height="wrap_content" android:text="."></Button>
			</LinearLayout>
		</LinearLayout>
		<LinearLayout android:orientation="horizontal" 
				android:layout_weight="1"
				android:layout_width="wrap_content" android:layout_height="match_parent">
			<Button android:layout_width="match_parent"
				android:layout_height="match_parent" android:text="="
				android:layout_weight="1"></Button>
		</LinearLayout>	
	</LinearLayout>
</LinearLayout>

2. Android之框架布局:

  框架布局(FrameLayout)是所有布局中最简单的一种。可以把框架布局看做一块在屏幕在预定的空白区域,可以填整块的元素到里面,如一张图片。所有的元素都会被固定在屏幕的左上角,不能为FrameLayout中的一个元素指定一个位置。后一个元素将会直接在前一个元素之上进行填充,把它们部分或全部挡住。第一个添加到框架布局的视图将显示在栈的底部,最后添加的视图会显示在最顶部。所以我们可以把框架布局看作一块画布,可以在上面从左上角开始直充图片或文字。

案例效果图及其结构分析:

此布局源码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
	<ImageView
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:src="@drawable/images_1">
	</ImageView>
	
	<TextView
		android:id="@+id/textView1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="You are so beautiful!"
		android:textAppearance="?android:attr/textAppearanceLarge"/>
	<Button
		android:id="@+id/button1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="bottom"
		android:text="Nice button"/>
	<ImageView
		android:id="@+id/imageView1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center_vertical|center_horizontal"
		android:src="@drawable/image"/>
</FrameLayout>

  框架布局的效率使得它是包含很少视图控件的屏幕的很好的选择(主屏幕,只有一个画布的游戏界面等)。有些时候其它低效的布局设计可以简化为一个更有效率的框架布局设计,而其它时候使用更专业的布局类型会更合适。当你想要堆叠视图时框架布局是一般的选择。  FrameLayout相对比较简单。因为这一点,很多其它布局类型和视图控件都是基于它的。例如,ScrollView就是一个在子内容太大而不能在布局界限内完全展示时出现滚动条的框架布局。所有主屏幕(Home屏幕)应用小工具都位于一个框架布局中。

抱歉!评论已关闭.