Chinaunix首页 | 论坛 | 博客
  • 博客访问: 688959
  • 博文数量: 112
  • 博客积分: 2486
  • 博客等级: 大尉
  • 技术积分: 1541
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-14 18:30
文章分类

全部博文(112)

文章存档

2012年(5)

2011年(48)

2010年(26)

2009年(33)

我的朋友

分类: Java

2011-09-15 22:20:16

初学Android的朋友总是会困惑于布局方面的问题,甚至会怀念旧式VB开发中的绝对定位的方式(那绝对是个悲剧)。其实Android的Layout功能还是很强劲的,几乎可以让你不废吹灰之力的完成所有的布局要求,本篇教程会对Android中各个Layout的使用进行一个汇总。
这是教程的第一部分,将会介绍最常用的2个Layout: LinearLayoutRelativeLayout
相关教程:教程:Android各种Layout特性和使用汇总(二)介绍了TableLayout和FrameLayout。

LinearLayout 线性布局 特性:

横向或纵向,每行或每列(根据方向)只能有1个对象,可以设置对象在LinearLayout中占位的权重。
看上去很简单,也确实很简单,但LinearLayout却是Android中使用最多的布局之一,尤其是在多个Linear嵌套使用的时候,就会显得异乎寻常的强大。

例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > android:text="Title" android:textSize="18dp" android:background="#AA3333FF" android:layout_width="fill_parent" android:layout_height="wrap_content" /> android:id="@+id/listViewInLinear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingBottom="5dp" android:layout_weight="1" /> android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:layout_weight="50" /> android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back" android:layout_weight="50" /> > >

下面是onCreate中ListView数据填充的方法,直接使用了APIDemo中的数据,用最简单的ArrayAdapter来实现。

1 2 3 4 5 6 7 ListView arrowList=(ListView) findViewById(R.id.listViewInLinear); arrowList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); //mString定义 private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", ... }

下面是运行截图,可以看到简单的2个LinearLayout嵌套外加layout_weight的使用,构造各分辨率自适应的布局是如此简单。
LinearLayout Sample截图
注意:同1个LinearLayout中可以设置多个子部件都有layout_weight属性,此时它们的大小会按weight的比例来进行分配,一般建议如果有多个weight,可以考虑让它们的和等于100。
另外,如果设置了layout_weight,那么与LinearLayout相同方向的大小设置不要设置fill_parent,而是设置wrap_content比较好,否则会发生问题。(具体情形遇见便知)

关键字:

orientation(方向),layout_weight(权重),嵌套

RelativeLayout 相对位置布局 特性:

布局内对象的位置可以相对于Layout本身来定位,也可以根据之前的对象来定位。非常适合用来表现多个对象对齐(或者故意不对齐?)的布局。
上面这段描述像不像什么都没说?其实RelativeLayout可以做的事情基本上都可以由多个LinearLayout来实现,但在条件适合的时候用RelativeLayout来实现会更方便,更强大。而且有些情况下几乎只有RelativeLayout才能实现了。
考虑以下这个布局的实现方式,
预想的布局
布局的第1行其实完全可以用LinearLayout来实现,只需要添加1个无文本的TextView在伸展区,并且设置layout_weight即 可,虽然有些麻烦,但仍然可以实现。但第2行如何实现呢?要求是新的按钮与Right1右对齐。而用RelativeLayout就可以轻松的实现,下面 是Layout定义文件:

例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 version="1.0" encoding="utf-8"?> xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent" > android:id="@+id/relaLeftButton" android:text="Left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> android:id="@+id/relaRight2Button" android:text="Right2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" /> android:id="@+id/relaRight1Button" android:text="Right1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/relaRight2Button" android:layout_toLeftOf="@id/relaRight2Button" /> android:id="@+id/relaRight3Button" android:text="Right3Long" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/relaRight1Button" android:layout_below="@id/relaRight1Button" /> >

在你使用了RelativeLayout之后,你会发现其中的控件都多了很多的layout_align开头的属性,有用于与父窗口对齐的,有用于和其它控件对齐和声明摆放位置的。如此便可以轻松的实现相对布局。下面是运行时截图:
Relative布局运行时截图
是不是达成要求了?虽然这是1个比较极端的例子,但在实际应用中,RelativeLayout的使用还是相当普遍的,通常用于登录画面之类的,元素不多,但要求布局相对宽松尽量利用空间的情况下。

关键字:

相对位置,对齐,摆放

阅读(4512) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~