第六讲:用户界面 View(二)

原文 http://android.yaohuiji.com/archives/date/2010/07/09

lesson6_view

本讲内容:常用Layout介绍: AbsoluteLayout和RelativeLayout

点此下载:Android学习指南第六讲源代码

3、绝对布局 AbsoluteLayout

绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。

下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:layout_y来指定子元素的纵横坐标。

<?xml version=”1.0″ encoding=”utf-8″?>
<AbsoluteLayout android:id=”@+id/AbsoluteLayout01″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android”
android:background=”#fff”>

<ImageView
android:src=”@drawable/android”
android:layout_y=”40dip”
android:layout_width=”wrap_content”
android:layout_x=”35dip”
android:id=”@+id/ImageView01″
android:layout_height=”wrap_content”>
</ImageView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:id=”@+id/TextView01″
android:text=”Android2.2 学习指南”
android:textColor=”#0f0″
android:textSize=”28dip”
android:layout_y=”330dip”
android:layout_x=”35dip
“>
</TextView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:id=”@+id/TextView02″
android:text=”图文并茂,理论清晰,操作性强”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_y=”365dip”
android:layout_x=”35dip
“>
</TextView>
</AbsoluteLayout>

让我们看一下在WQVGA的模拟器下的显示效果:

image

再在WVGA800的模拟器下看看显示效果:

image

Tip: 在绝对定位中,如果子元素不设置layout_x和layout_y,那么它们的默认值是0,也就是说它会像在FrameLayout一样这个元素会出现在左上角。

4、相对布局 RelativeLayout

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

下面我们用相对布局再做一次上面的例子,首先放置一个图片,其它两个文本分别相对上一个元素定位:

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout android:id=”@+id/RelativeLayout01″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#fff”
xmlns:android=”http://schemas.android.com/apk/res/android”>

<ImageView android:id=”@+id/ImageView01″
android:src=”@drawable/android”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”40dip”
>
</ImageView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView01″
android:text=”Android2.2 学习指南”
android:textColor=”#0f0″
android:textSize=”28dip”
android:layout_below=”@id/ImageView01″
android:layout_centerHorizontal=”true”

android:layout_marginTop=”10dip”>
</TextView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView02″
android:text=”图文并茂,理论清晰,操作性强”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_below=”@id/TextView01″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”5dip
“>
</TextView>
</RelativeLayout>

让我们看一下在WQVGA的模拟器下的显示效果:

image

再看一下在更大屏幕(WVGA800)模拟器上的显示效果:

image

从上图可以看到界面效果基本保持了一致,而不是像绝对定位一样龟缩在左上角;同学们看到自动缩放的功能是采用了dip做单位带来的好处。关于dip,不懂的同学可以看我在开发小知识里写的专门的文章。

下面介绍一下RelativeLayout用到的一些重要的属性:

第一类:属性值为true或false
android:layout_centerHrizontal                                           水平居中
android:layout_centerVertical                                            垂直居中
android:layout_centerInparent                                           相对于父元素完全居中
android:layout_alignParentBottom                                     贴紧父元素的下边缘
android:layout_alignParentLeft                                          贴紧父元素的左边缘
android:layout_alignParentRight                                        贴紧父元素的右边缘
android:layout_alignParentTop                                          贴紧父元素的上边缘
android:layout_alignWithParentIfMissing                            如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below                          在某元素的下方
android:layout_above                          在某元素的的上方
android:layout_toLeftOf                       在某元素的左边
android:layout_toRightOf                     在某元素的右边

android:layout_alignTop                      本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft                      本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom                 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight                    本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom              离某元素底边缘的距离
android:layout_marginLeft                   离某元素左边缘的距离
android:layout_marginRight                 离某元素右边缘的距离
android:layout_marginTop                   离某元素上边缘的距离

我们再把上面的例子重新做一遍,这一次多放一些属性在里面,大家试验一下:

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout android:id=”@+id/RelativeLayout01″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#cfff” 色彩的设置是argb,第一个c是透明度
xmlns:android=”http://schemas.android.com/apk/res/android”>

<ImageView android:id=”@+id/ImageView01″
android:src=”@drawable/android”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”40dip”
android:layout_centerHorizontal=”true”>
</ImageView>

<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView01″
android:text=”Android2.2 学习指南”
android:textColor=”#0f0″
android:textSize=”28dip”
android:layout_below=”@id/ImageView01″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”10dip”>
</TextView>

<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView02″
android:text=”图文并茂,理论清晰,操作性强”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_below=”@id/TextView01″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”5dip”>
</TextView>

<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView03″
android:text=”alignTop”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_alignTop=”@id/ImageView01″  和ImageView01上边缘对齐
android:layout_centerHorizontal=”true”>
</TextView>

<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView04″
android:text=”alignLeft”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_alignLeft=”@id/ImageView01″
android:layout_centerHorizontal=”true”>
</TextView>

<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView05″
android:text=”alignRight”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_alignRight=”@id/ImageView01″
android:layout_centerHorizontal=”true”>
</TextView>

<TextView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:id=”@+id/TextView06″
android:text=”alignBottom”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_alignBottom=”@id/ImageView01″
android:layout_centerHorizontal=”true”>
</TextView>
</RelativeLayout>

image

好吧今天就讲到这里。

 

学习Android界面设计的超级利器HierarchyView.bat

09 JUL

存在问题: 
我看到别人的程序,想学习人家的UI布局是怎么做的,如何能窥探一二?

存在问题: 
在学习ViewGroup和Layout时我们可能会有一个疑问,如果我在Xml布局文件中不放置Layout,直接放TextView等组件的时候,它是用什么方式布局的?

解决方法: 
Android的SDK中自带了一个查看UI布局层级结构的工具:HierarchyView.bat。在模拟器运行的情况下,使用该工具可以将当前的Activity中的UI组件们以对象树的形式展现出来,每一个组件所包含的属性也能窥探得到。在对象树上的任意节点可以看到该节点及以下节点的显示效果。使用HierarchyView能深入全面的理解xml布局文件,更可以通过它来学习别人优秀的布局技巧。

image 
(看到那个不起眼的hierachyview.bat文件没,哎,竟然它是学习Android界面设计的超级利器)

具体使用方法:待模拟器及想观察的程序启动后,双击Hirerachyview.bat,点击Load View Hierarchy

image 

现在我们先解决第一个问题:分析AndroidQQ的登录界面组成。

image
从图中可以看到QQ登录界面中的核心部分是用相对布局摆放的,甚至可以看到有的TextView还用的是IDE自动起的ID:TextView01。

下面我们解决第二个问题:找到默认的布局方式是什么?

我们建一个新项目,Main.xml中只放一个TextView,

<?xml version="1.0" encoding="utf-8"?> 
<TextView android:text="hi" android:id="@+id/TV" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"></TextView>

 

看看他的布局层次是什么样的:

image

我们可以看到顶级元素是个PhoneWindow,其下有一个线性布局LinearLayout,再下面有一个应用程序标题条,

image 

 

标题条是由一个帧布局FrameLayout和一个TextView组成。 
我们自己写的TextView也放在了一个Layout里,这个Layout就是ID叫做content的FrameLayout。

image

接下来我们再创建一个带有LinearLayout的程序,看看他的层次结构来确定一下,那个ID是content的FrameLayout是固有的还是因为没布局元素由系统自动加上去的。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    <TextView android:text="haha" android:id="@+id/TextView01" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
</LinearLayout>

下面是新程序的UI界面层次结构图:

image

这一次我们同样看到了那个Id是content的Framelayout:) 这说明它是窗口布局结构里固有的。

 

好了这次的内容就到这里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值