本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


FrameLayout就是屏幕上的一个“定位器”,可以使用它去显示一个单一的视图。被添加到FrameLayout上的视图views总是被固定在这个布局的左上角。考虑以下的代码:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/RLayout"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:id="@+id/lblComments"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_alignParentTop="true"
  12. android:text="Hello,Android!"/>
  13. <FrameLayout
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignLeft="@+id/lblComments"
  17. android:layout_below="@+id/lblComments"
  18. android:layout_centerHorizontal="true">
  19. <ImageView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:src="@drawable/droid">
  23. </ImageView>
  24. </FrameLayout>
  25. </RelativeLayout>
这里,在RelativeLayout中内嵌了一个FrameLayuout,在FrameLayuout中内嵌了一个ImageView。效果图:

但是,如果想要在这个FrameLayuout中添加另外的view(比如一个Button),那么这个view就会重叠在“之前的”view上面(本例中是显示图片的ImageView)。代码:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/RLayout"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <TextView
  7. android:id="@+id/lblComments"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:layout_alignParentTop="true"
  12. android:text="Hello,Android!"/>
  13. <FrameLayout
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignLeft="@+id/lblComments"
  17. android:layout_below="@+id/lblComments"
  18. android:layout_centerHorizontal="true">
  19. <ImageView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:src="@drawable/droid">
  23. </ImageView>
  24. <Button
  25. android:layout_width="124dp"
  26. android:layout_height="wrap_content"
  27. android:text="PrintPicture"/>
  28. </FrameLayout>
  29. </RelativeLayout>
最终效果图:


更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android系统源代码目录与系统目录
  3. mtk android SD 配置 (MT6572)
  4. Android(安卓)Handler机制4--MessageQueue简介
  5. Android(安卓)工具类 —— 判断网络连接状态、GPS是否打开、基站
  6. Android自带的几种Activirty
  7. 网页中android Toast效果的实现
  8. Android(安卓)学习笔记 databinding简单使用:使用databinding在li
  9. android与phonegap的相互交互

随机推荐

  1. android 下载文件(多任务,断点下载.....)
  2. ANDROID中EDITTEXT设置输入条件
  3. 供大家学习的Android开源项目
  4. Android(安卓)通过手说tts中文语音包实现
  5. Android开发网上的一些重要知识点_4
  6. android 万能视频播放器
  7. Android:从零开始打造自己的深度链接库(二):A
  8. Android学习11--事件处理
  9. Android(安卓)小项目之--Mini音乐播放器
  10. Android(安卓)四大组件之Activity