(四)用户界面 View

一、什么是View
我们上节课说,Activity是Android程序的显示层,每一个显示窗口都是一个Activity;可是Activity本身无法显示在屏幕上,我们可以把它理解成是一个抽象层,一个壳子;就譬如一个JSP页面,它本身并没有显示出来任何东西,负责显示的是他生成的HTML标签。 那么Android里谁才是真正显示出来的部分?--是View和ViewGroup,而ViewGroup其实也是View的子类
有了上述的概念,我们现在可以讲明白一个Activity中的显示元素是如何显示出来的了。首先UI组件是按层次结构来 由外到内的方式逐步展示的。要将一个屏幕元素层次树绑定在一个屏幕上显示,Activity会调用它的 setContentView()方法并且传入这个层次树的根节点引用。当Activity被激活并且获得焦点时,系统会通知activity并且请求根节点去计算并绘制树,根节点就会请求它的子节点去绘制它们自己。 每个树上的ViewGroup节点会负责绘制它的子节点。ViewGroup会计算它的有效空间,布局所有的子显示对象,并最终调用所有的子显示对象的 Draw()方法来绘制显示对象。各个子显示对象可以向父对象请求它们在布局中的大小和位置,但最终决定各个子显示对象的大小和位置的是父对象。
Android程序借助View和ViewGroup对象来构建用户界面。Android提供了比HTML多得多的,现成的用户界面组件,譬如现在网站上常见的五角星评分效果组件RatingBar。RatingBar的显示效果如下图所示:
1.png
二、常用Layout介绍
ViewGroup是个特殊的View,它继承于Android.view.View。它的功能就是装载和管理下一层的View对象或ViewGroup对象,也就说他是一个 容纳其它元素的的容器。ViewGroup是布局管理器(layout)及view容器的基类。 ViewGroup中,还定义了一个嵌套类ViewGroup. LayoutParams。这个类定义了一个显示对象的位置、大小等属性,view通过LayoutParams中的这些属性值来告诉父级,它们将如何放置。
2.png
ViewGroup是一个抽象类,所以真正充当容器的是他的子类们。我们在这里将介绍 帧布局FrameLayout,线性布局LinearLayout,绝对布局AbsoluteLayout,相对布局RelativeLayout,表格布局TableLayout等几个常用布局,大约要分3讲讲完。
1、帧布局 FrameLayout:
是最简单的一个布局对象。在他里面的的所有显示对象都将固定在屏幕的左上角, 不能指定位置,但允许有多个显示对象,只是后一个会直接覆盖在前一个之上显示,会把前面的组件部分或全部挡住。下图的例子里,FrameLayout中放了3个ImageView组件,第一个是蓝色的,第二个是绿色的,第三个是树状图(透明的png格式)。 ImageView就相当于Html中的img标签,接下来会讲到这个组件。
下面看一个FrameLayout的例子:

  1. <?xml version=”1.0″ encoding=”utf-8″?><FrameLayout android:id=”@+id/FrameLayout01″
  2. android:layout_width=”fill_parent” android:layout_height=”fill_parent”
  3. xmlns:android=”http://schemas.android.com/apk/res/android”><ImageView android:id=”@+id/ImageView01″ android:src=”@drawable/p1″
  4. android:layout_width=”wrap_content” android:layout_height=”wrap_content”></ImageView><ImageView android:id=”@+id/ImageView02″ android:src=”@drawable/p2″
  5. android:layout_width=”wrap_content” android:layout_height=”wrap_content”></ImageView><ImageView android:id=”@+id/ImageView03″ android:src=”@drawable/p3″
  6. android:layout_width=”wrap_content” android:layout_height=”wrap_content”></ImageView></FrameLayout>
复制代码


完整的代码在PPT附带的目录中,需要的朋友可以留言向我索要。
3.png
(FrameLayout的显示效果)
2、线性布局 LinearLayout:
线性布局是所有布局中最常用的类之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类。 LinearLayout可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)
下面看一个LinearLayout的例子:别被例子的长度吓住,仔细看一下其实就是一个LinearLayout中放5个TextView标签而已, TextView相当于Html标签中的Label

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:orientation=”vertical”
  4. android:layout_width=”fill_parent”
  5. android:layout_height=”fill_parent”
  6. android:gravity=”center_horizontal”
  7. >
  8. <TextView
  9. android:layout_width=”fill_parent”
  10. android:layout_height=”wrap_content”
  11. android:text=”给小宝宝起个名字:”
  12. android:textSize=”20px”
  13. android:textColor=”#0ff”
  14. android:background=”#333″/>
  15. <TextView
  16. android:layout_width=”wrap_content”
  17. android:layout_height=”wrap_content”
  18. android:text=”遥遥是男孩的小名”
  19. android:textSize=”20px”
  20. android:textColor=”#0f0″
  21. android:background=”#eee”
  22. android:layout_weight=”3″
  23. />
  24. <TextView
  25. android:layout_width=”wrap_content”
  26. android:layout_height=”wrap_content”
  27. android:text=”瑶瑶是女孩的小名”
  28. android:textColor=”#00f”
  29. android:textSize=”20px”
  30. android:background=”#ccc”
  31. android:layout_weight=”1″
  32. /><TextView
  33. android:layout_width=”fill_parent”
  34. android:layout_height=”wrap_content”
  35. android:text=”海因是男孩的大名”
  36. android:textColor=”#f33″
  37. android:textSize=”20px”
  38. android:background=”#888″
  39. android:layout_weight=”1″
  40. />
  41. <TextView
  42. android:layout_width=”fill_parent”
  43. android:layout_height=”wrap_content”
  44. android:text=”海音是女孩的大名”
  45. android:textColor=”#ff3″
  46. android:textSize=”20px”
  47. android:background=”#333″
  48. android:layout_weight=”1″
  49. />
  50. </LinearLayout>
复制代码


下图是显示效果:

4.png


3、绝对布局 AbsoluteLayout
绝对定位AbsoluteLayout,又可以叫做坐标布局,可以 直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的 适应性会比较差
下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:layout_y来指定子元素的纵横坐标。

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <AbsoluteLayout android:id=”@+id/AbsoluteLayout01″
  3. android:layout_width=”fill_parent”
  4. android:layout_height=”fill_parent”
  5. xmlns:android=”http://schemas.android.com/apk/res/android”
  6. android:background=”#fff”><ImageView
  7. android:src=”@drawable/android”
  8. android:layout_y=”40dip”
  9. android:layout_width=”wrap_content”
  10. android:layout_x=”35dip”
  11. android:id=”@+id/ImageView01″
  12. android:layout_height=”wrap_content”>
  13. </ImageView>
  14. <TextView
  15. android:layout_height=”wrap_content”
  16. android:layout_width=”fill_parent”
  17. android:id=”@+id/TextView01″
  18. android:text=”Android2.2 学习指南”
  19. android:textColor=”#0f0″
  20. android:textSize=”28dip”
  21. android:layout_y=”330dip”
  22. android:layout_x=”35dip“>
  23. </TextView>
  24. <TextView
  25. android:layout_height=”wrap_content”
  26. android:layout_width=”fill_parent”
  27. android:id=”@+id/TextView02″
  28. android:text=”图文并茂,理论清晰,操作性强”
  29. android:textColor=”#333″
  30. android:textSize=”18dip”
  31. android:layout_y=”365dip”
  32. android:layout_x=”35dip“>
  33. </TextView>
  34. </AbsoluteLayout>
复制代码


让我们看一下在WQVGA的模拟器下的显示效果:
1.png
再在WVGA800的模拟器下看看显示效果:
2.png
Tip: 在绝对定位中,如果子元素不设置layout_x和layout_y,那么它们的默认值是0,也就是说它会像在FrameLayout一样这个元素会出现在左上角。
4、相对布局 RelativeLayout
相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大, 使用相对布局时要多做些测试
下面我们用相对布局再做一次上面的例子,首先放置一个图片,其它两个文本分别相对上一个元素定位:

  1. <?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout android:id=”@+id/RelativeLayout01″
  2. android:layout_width=”fill_parent”
  3. android:layout_height=”fill_parent”
  4. android:background=”#fff”
  5. xmlns:android=”http://schemas.android.com/apk/res/android”><ImageView android:id=”@+id/ImageView01″
  6. android:src=”@drawable/android”
  7. android:layout_width=”fill_parent”
  8. android:layout_height=”wrap_content”
  9. android:layout_marginTop=”40dip”
  10. >
  11. </ImageView>
  12. <TextView
  13. android:layout_height=”wrap_content”
  14. android:layout_width=”wrap_content”
  15. android:id=”@+id/TextView01″
  16. android:text=”Android2.2 学习指南”
  17. android:textColor=”#0f0″
  18. android:textSize=”28dip”
  19. android:layout_below=”@id/ImageView01″
  20. android:layout_centerHorizontal=”true”
  21. android:layout_marginTop=”10dip”>
  22. </TextView>
  23. <TextView
  24. android:layout_height=”wrap_content”
  25. android:layout_width=”wrap_content”
  26. android:id=”@+id/TextView02″
  27. android:text=”图文并茂,理论清晰,操作性强”
  28. android:textColor=”#333″
  29. android:textSize=”18dip”
  30. android:layout_below=”@id/TextView01″
  31. android:layout_centerHorizontal=”true”
  32. android:layout_marginTop=”5dip“>
  33. </TextView>
  34. </RelativeLayout>
复制代码


让我们看一下在WQVGA的模拟器下的显示效果:
3.png
再看一下在更大屏幕(WVGA800)模拟器上的显示效果:
4.png
从上图可以看到界面效果基本保持了一致,而不是像绝对定位一样龟缩在左上角;同学们看到 自动缩放的功能是采用了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                   离某元素上边缘的距离
我们再把上面的例子重新做一遍,这一次多放一些属性在里面,大家试验一下:

  1. <?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout android:id=”@+id/RelativeLayout01″
  2. android:layout_width=”fill_parent”
  3. android:layout_height=”fill_parent”
  4. android:background=”#cfff” 色彩的设置是argb,第一个c是透明度
  5. xmlns:android=”http://schemas.android.com/apk/res/android”><ImageView android:id=”@+id/ImageView01″
  6. android:src=”@drawable/android”
  7. android:layout_width=”wrap_content”
  8. android:layout_height=”wrap_content”
  9. android:layout_marginTop=”40dip”
  10. android:layout_centerHorizontal=”true”>
  11. </ImageView><TextView
  12. android:layout_height=”wrap_content”
  13. android:layout_width=”wrap_content”
  14. android:id=”@+id/TextView01″
  15. android:text=”Android2.2 学习指南”
  16. android:textColor=”#0f0″
  17. android:textSize=”28dip”
  18. android:layout_below=”@id/ImageView01″
  19. android:layout_centerHorizontal=”true”
  20. android:layout_marginTop=”10dip”>
  21. </TextView><TextView
  22. android:layout_height=”wrap_content”
  23. android:layout_width=”wrap_content”
  24. android:id=”@+id/TextView02″
  25. android:text=”图文并茂,理论清晰,操作性强”
  26. android:textColor=”#333″
  27. android:textSize=”18dip”
  28. android:layout_below=”@id/TextView01″
  29. android:layout_centerHorizontal=”true”
  30. android:layout_marginTop=”5dip”>
  31. </TextView><TextView
  32. android:layout_height=”wrap_content”
  33. android:layout_width=”wrap_content”
  34. android:id=”@+id/TextView03″
  35. android:text=”alignTop”
  36. android:textColor=”#333″
  37. android:textSize=”18dip”
  38. android:layout_alignTop=”@id/ImageView01″  和ImageView01上边缘对齐
  39. android:layout_centerHorizontal=”true”>
  40. </TextView><TextView
  41. android:layout_height=”wrap_content”
  42. android:layout_width=”wrap_content”
  43. android:id=”@+id/TextView04″
  44. android:text=”alignLeft”
  45. android:textColor=”#333″
  46. android:textSize=”18dip”
  47. android:layout_alignLeft=”@id/ImageView01″
  48. android:layout_centerHorizontal=”true”>
  49. </TextView><TextView
  50. android:layout_height=”wrap_content”
  51. android:layout_width=”wrap_content”
  52. android:id=”@+id/TextView05″
  53. android:text=”alignRight”
  54. android:textColor=”#333″
  55. android:textSize=”18dip”
  56. android:layout_alignRight=”@id/ImageView01″
  57. android:layout_centerHorizontal=”true”>
  58. </TextView><TextView
  59. android:layout_height=”wrap_content”
  60. android:layout_width=”wrap_content”
  61. android:id=”@+id/TextView06″
  62. android:text=”alignBottom”
  63. android:textColor=”#333″
  64. android:textSize=”18dip”
  65. android:layout_alignBottom=”@id/ImageView01″
  66. android:layout_centerHorizontal=”true”>
  67. </TextView>
  68. </RelativeLayout>
复制代码


5.png

5、表格布局 TableLayout
表格布局TableLayout以行列的形式管理子元素,每一行是一个TableRow布局对象,当然也可以是普通的View对象,TableRow离每放一个元素就是一列,总列数由列数最多的那一行决定。
我们看一个例子:

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <TableLayout android:id=”@+id/TableLayout01″
  3. android:layout_width=”fill_parent” android:layout_height=”fill_parent”
  4. android:stretchColumns=”0″ xmlns:android=”http://schemas.android.com/apk/res/android”><TableRow android:layout_width=”fill_parent”
  5. android:layout_height=”20dip”>
  6. <TextView android:text=”色彩透明度测试” android:textSize=”18dip”
  7. android:layout_span=”2″ 合并两列
  8. android:layout_gravity=”center”
  9. android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
  10. </TextView>
复制代码


再看一下显示效果:
1.png
其中 android:stretchColumns=”0″ 作用是让第一列可以扩展到所有可用空间;下面我们讲一下TableLayout几个重要的属性:
collapseColumns – 设置隐藏那些列,列ID从0开始,多个列的话用”,”分隔
stretchColumns – 设置自动伸展那些列,列ID从0开始,多个列的话用”,”分隔
shrinkColumns -设置自动收缩那些列,列ID从0开始,多个列的话用”,”分隔
可以用”*”来表示所有列,同一列可以同时设置为shrinkable和stretchable。
我们再举一个例子来看一下:

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:layout_width=”fill_parent”
  4. android:layout_height=”fill_parent”
  5. android:stretchColumns=”1″>   第二列自动伸展<TableRow>
  6. <TextView
  7. android:layout_column=”1″     我是第二列
  8. android:text=”打开…”
  9. android:padding=”3dip” /> 元素内容与边界之间保留3dip的距离
  10. <TextView
  11. android:text=”Ctrl-O”
  12. android:gravity=”right”
  13. android:padding=”3dip” />
  14. </TableRow><TableRow>
  15. <TextView
  16. android:layout_column=”1″
  17. android:text=”保存…”
  18. android:padding=”3dip” />
  19. <TextView
  20. android:text=”Ctrl-S”
  21. android:gravity=”right” 元素本身的内容向右对齐
  22. android:padding=”3dip” />
  23. </TableRow><TableRow>
  24. <TextView
  25. android:layout_column=”1″
  26. android:text=”另存为…”
  27. android:padding=”3dip” />
  28. <TextView
  29. android:text=”Ctrl-Shift-S”
  30. android:gravity=”right”
  31. android:padding=”3dip” />
  32. </TableRow><View
  33. android:layout_height=”2dip”
  34. android:background=”#FF909090″ /><TableRow>
  35. <TextView
  36. android:text=”X”
  37. android:padding=”3dip” />
  38. <TextView
  39. android:text=”导入…”
  40. android:padding=”3dip” />
  41. </TableRow><TableRow>
  42. <TextView
  43. android:text=”X”
  44. android:padding=”3dip” />
  45. <TextView
  46. android:text=”导出…”
  47. android:padding=”3dip” />
  48. <TextView
  49. android:text=”Ctrl-E”
  50. android:gravity=”right”
  51. android:padding=”3dip” />
  52. </TableRow><View
  53. android:layout_height=”2dip”
  54. android:background=”#FF909090″ /><TableRow>
  55. <TextView
  56. android:layout_column=”1″
  57. android:text=”退出”
  58. android:padding=”3dip” />
  59. </TableRow>
  60. </TableLayout>
复制代码


下面是显示效果:
2.png

我加粗显示的地方都有解释,大家可以留意一下。
Tip:TableRow也是一个Layout,里面的元素会水平排列,如果TableRow的父元素不是TableLayout的话,那么他会表现的像一个LinearLayout。
接下来会讲2个比较复杂的布局,然后讲一些常用的View Widget。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值