AndroidLayout.docx

上传人:b****5 文档编号:7450666 上传时间:2023-05-11 格式:DOCX 页数:17 大小:104.30KB
下载 相关 举报
AndroidLayout.docx_第1页
第1页 / 共17页
AndroidLayout.docx_第2页
第2页 / 共17页
AndroidLayout.docx_第3页
第3页 / 共17页
AndroidLayout.docx_第4页
第4页 / 共17页
AndroidLayout.docx_第5页
第5页 / 共17页
AndroidLayout.docx_第6页
第6页 / 共17页
AndroidLayout.docx_第7页
第7页 / 共17页
AndroidLayout.docx_第8页
第8页 / 共17页
AndroidLayout.docx_第9页
第9页 / 共17页
AndroidLayout.docx_第10页
第10页 / 共17页
AndroidLayout.docx_第11页
第11页 / 共17页
AndroidLayout.docx_第12页
第12页 / 共17页
AndroidLayout.docx_第13页
第13页 / 共17页
AndroidLayout.docx_第14页
第14页 / 共17页
AndroidLayout.docx_第15页
第15页 / 共17页
AndroidLayout.docx_第16页
第16页 / 共17页
AndroidLayout.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

AndroidLayout.docx

《AndroidLayout.docx》由会员分享,可在线阅读,更多相关《AndroidLayout.docx(17页珍藏版)》请在冰点文库上搜索。

AndroidLayout.docx

AndroidLayout

AndroidLayout有五大布局对象,分别是

FrameLayout(帧布局)

LinearLayout(线性布局)

AbsoluteLayout(绝对布局)

RelativeLayout(相对布局)

TableLayout(表格布局)

 

FrameLayout

该布局container可以用来占有屏幕的某块区域来显示单一的对象,可以包含有多个widgets或者是container,但是所有被包含的widgets或者是container必须被固定到屏幕的左上角,并且一层覆盖一层,不能通过为一个widgets或者是container指定一个位置。

Container所包含的widgets或者是container的队列是采用的堆栈的结构,最后加进来的widgets或者是container显示在最上面。

所以后一个widgets或者是container将会直接覆盖在前一个widgets或者是container之上,把它们部份或全部挡住(除非后一个widgets或者是container是透明的,必须得到FrameLayoutContainer的允许)。

其中Main.xml代码如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/hello"

/>

android:

textColor="#0000FF"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="@string/hello2"

/>

Strings.xml代码如下:

xmlversion="1.0"encoding="utf-8"?

>

HelloWorld,LayoutTestActivity!

LayoutTest

HelloWorld,IcannotbeLOST!

如下图,其中TextViewHello2是最后FrameLayout最后加入的,所以该TextView应该是覆盖整个屏幕的,TextViewhello1被其覆盖:

 

LinearLayout

LinearLayout是一个盒子模型(BoxModel),以垂直或水平的方向,按照相对位置来排列所有的widgets或者其他的containers。

所有被包含的widgets或者是containers都被堆放在container之后,因此一个垂直列表的每一行只会有一个widget或者是container,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子控件的高度加上边框高度)。

LinearLayout保持其所包含的widget或者是container之间的间隔以及互相对齐(相对一个控件的右对齐、中间对齐或者左对齐)。

LinearLayout还支持为其包含的widget或者是container指定填充权值。

好处就是允许其包含的widget或者是container可以填充屏幕上的剩余空间。

这也避免了在一个大屏幕中,一串widgets或者是containers挤成一堆的情况,而是允许他们放大填充空白。

剩余的空间会按这些widgets或者是containers指定的权值比例分配屏幕。

默认的weight值为0,表示按照widgets或者是containers实际大小来显示,若高于0的值,则将Container剩余可用空间分割,分割大小具体取决于每一个widget或者是container的layout_weight及该权值在所有widgets或者是containers中的比例。

例如,如果有三个文本框,其中两个指定的权值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大,按实际大小来显示。

如果前两个文本框的取值一个为2,一个为1,显示第三个文本框后剩余的空间的2/3给权值为2的,1/3大小给权值为1的。

也就是权值越大,重要度越大。

如果LinearLayout包含子LinearLayout,子LinearLayout之间的权值越大的,重要度则越小。

如果有LinearLayoutA包含LinearLayoutC,D,C的权值为2,D的权值为1,则屏幕的2/3空间分给权值为1的D,1/3分给权值为2的C。

在LinearLayout嵌套的情况下,子LinearLayout必须要设置权值,否则默认的情况是未设置权值的子LinearLayout占据整个屏幕。

我们看一下效果图:

其中main.xml代码如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent">

android:

id="@+id/lineLayout1"

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

layout_weight="2">

-如果未设置权值,则lineLayout1占据整个屏幕显示->

android:

text="blockwithweight2issmallerthantheblockwithweight1"

android:

gravity="center_horizontal"

android:

textSize="8pt"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

/>

android:

id="@+id/lineLayout1"

android:

orientation="horizontal"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

layout_weight="1">

android:

id="@+id/red"

android:

text="red"

android:

gravity="center_horizontal"

android:

background="#aa0000"

android:

layout_width="wrap_content"

android:

layout_height="fill_parent"

android:

layout_weight="1"/>

android:

id="@+id/white"

android:

text="white"

android:

gravity="center_horizontal"

android:

background="#000000"

android:

layout_width="wrap_content"

android:

layout_height="fill_parent"

/>

android:

id="@+id/green"

android:

text="green"

android:

gravity="center_horizontal"

android:

background="#00aa00"

android:

layout_width="wrap_content"

android:

layout_height="fill_parent"

android:

layout_weight="1"/>

 

下面一个例子用来解释一下LinearLayout的一些属性的设置,在LinearLayout中包含有两个RadioGroup,上面的RadioGroup设置了一行的RadioButton,如android:

orientation="horizontal",下面的一个设置了一列的RadioButton。

每个RadioGroup都在其周围设置了padding用来和其他的RadioGroup区分开。

这两个RadioGroup的layout_height和layout_width都设置为Wrap_content,这些RadioGroup只会按照实际显示大小来显示。

Main.xml的内容如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_height="fill_parent"

android:

layout_width="fill_parent"

>

id="@+id/orientation"

android:

orientation="horizontal"

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

padding="5px">

id="@+id/horizotal"

android:

text="horizontal"/>

id="@+id/vertical"

android:

text="vertical"/>

id="@+id/gravity"

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

padding="5px">

id="@+id/left"

android:

text="left"/>

id="@+id/center"

android:

text="center"/>

id="@+id/right"

android:

text="right"/>

具体示图如下:

下面将通过在Activity里面动态修改LinearLayout

(二)中相应的属性来改变屏幕布局,代码如下:

packagecom.hemi.LayoutTest;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Gravity;

importandroid.text.TextWatcher;

importandroid.widget.LinearLayout;

importandroid.widget.RadioGroup;

importandroid.widget.EditText;

publicclassLayoutTestActivityextendsActivityimplementsRadioGroup.OnCheckedChangeListener{

RadioGrouporitentation;

RadioGroupgravity;

/**Calledwhentheactivityisfirstcreated.*/

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

oritentation=(RadioGroup)findViewById(R.id.orientation);

oritentation.setOnCheckedChangeListener(this);

gravity=(RadioGroup)findViewById(R.id.gravity);

gravity.setOnCheckedChangeListener(this);

}

publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){

if(group==oritentation){

if(checkedId==R.id.horizotal){

oritentation.setOrientation(LinearLayout.HORIZONTAL);

gravity.setOrientation(LinearLayout.HORIZONTAL);

}else{

oritentation.setOrientation(LinearLayout.VERTICAL);

gravity.setOrientation(LinearLayout.VERTICAL);

}

}else{

if(checkedId==R.id.left){

oritentation.setGravity(Gravity.LEFT);

gravity.setGravity(Gravity.LEFT);

}elseif(checkedId==R.id.right){

oritentation.setGravity(Gravity.RIGHT);

gravity.setGravity(Gravity.RIGHT);

}else{

oritentation.setGravity(Gravity.CENTER_HORIZONTAL);

gravity.setGravity(Gravity.CENTER_HORIZONTAL);

}

}

}

}

通过点击Oritentation和Gravity的RadioGroup来控制布局,默认的布局由main.xml来配置。

点击horizontal,vertical或者center和right可改变布局,示图如下:

 

RalativeLayout

我们从RalativeLayout可以知道,该布局的父子Container中所有的Widgets之间的位置都是相对的。

你可以把WidgetA放在Widget的左下方,或者放在Widget的底边并和Container的底边之间。

它允许为Container或者widget指定其相对于其他widgets或者父Container的位置。

Container或者widgets是按顺序排列的,如果第一个Container或者widget在屏幕的中央,那么相对于其的其他Container和widgets将以屏幕的中央来排列。

如果使用XML的Layout文件来定义,被引用的Container或者widgets必须被定义。

在XML的layout文件中,我们就能够引用同一文件的Widgets,并且可以标识其他Widgets的相对位置。

标识一个Widgets在其Container中的位置,我们有下面的属性参数可以用,这些参数的值可以使true或者false:

1).android:

layout_alignParentTop:

表示widget的顶部和Container的顶部重合。

2).android:

layout_alignParentBottom:

表示widget的底部和Container的底部重合。

3).android:

layout_alignParentLeft:

表示widget的左边和Container的左边重合。

4).android:

layout_alignParentRight:

表示widget的右边和Container的右边重合。

5).android:

layout_centerHorizontal:

表示widget处于Container水平方向上的中间。

6).android:

layout_centerVertical:

表示widget处于Container垂直方向上的中间。

7).android:

layout_centerInParent:

表示widget处于Container平面上的正中间。

标识同一个Container中的不同的widgets之间的相对位置时,可有用如下的属性参数,参数值是其他widgets的应用。

相对位置上的widgets的引用的设定如下:

首先必须给所有的被引用的widgets赋予标示符,通过android:

id的属性,格式如@+id/widget1,然后在其他widget引用的时候使用同样的标示符,格式如@id/widget1,例:

如果widgetA标识为@+id/widgetA,则widgetB在其属性里面通过@id/widgetA引用widgetA。

1).android:

layout_above:

表示该widget必须位于参数值标识的widget的上方。

2).android:

layout_below:

表示该widget必须位于参数值标识的widget的下方。

3).android:

layout_toLeftOf:

表示该widget必须位于参数值标识的widget的左方。

4).android:

layout_toLeftOf:

表示该widget必须位于参数值标识的widget的右方。

5).android:

layout_alignTop:

表示该widget的顶部必须参数值标识的widget的顶部重合。

6).android:

layout_alignBottom:

表示该widget的底部必须参数值标识的widget的底部重合。

7).android:

layout_alignLeft:

表示该widget的左边必须参数值标识的widget的左边重合。

8).android:

layout_alignRight:

表示该widget的右边必须参数值标识的widget的右边重合。

9).android:

layout_alignBaseLine:

表示该widget的BaseLine必须参数值标识的widget的BaseLine重合。

这个主要用于Label或者其他包含文本的widgets。

在我们引用widget时不能引用该文件中尚未定义的widget。

具体的示例如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

padding="5px"

>

id="@+id/label"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="URL:

"

android:

paddingTop="15px"/>

id="@+id/entry"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

layout_toRightOf="@id/label"

android:

layout_alignBaseline="@id/label"

/>

id="@+id/ok"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@id/entry"

android:

layout_alignRight="@id/entry"

android:

text="OK"

/>

id="@+id/cancel"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_toLeftOf="@id/ok"

android:

layout_alignTop="@id/ok"

android:

text="cancel"

/>

示图如下:

TableLayout

Android的TableLayout的布局就像Html的表格一样,可以根据我们的说明来安排widgets的位置。

我们可以自己控制屏幕的行数和列数,而每列可以根据包含的内容进行伸缩。

通常情况下,TableLayout有多个TableRow组成,每个TableRow就是一行,定义几个TableRow就是定义几行。

TableLayout不会显示行或者列或者cell的边线。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2