`
king_tt
  • 浏览: 2112010 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【Android 开发教程】FrameLayout帧布局

 
阅读更多

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

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


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

  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)。代码:

  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>
最终效果图:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics