sarin 发表于 2013-1-30 04:08:02

Android学习笔记12:框架布局管理器FrameLayout

接上文
    框架布局管理器是Android布局管理器之一,之前并没有接触过。简单来说,框架布局管理器是将组件都放在屏幕的左上角,所有的组件是层叠显示的。首先来看一下FrameLayout的文档:
http://dl.iteye.com/upload/attachment/0074/7771/9ee4e13c-43dd-328d-a1c6-8a71bcb5c2d6.jpg
    那么它的继承结构为:
java.lang.Object
   ↳ android.view.View
    ↳ android.view.ViewGroup
   ↳ android.widget.FrameLayout
    这和LinearLayout是类似的。下面我们创建一个项目来看看FrameLayout:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageView      android:id="@+id/img"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:contentDescription="这是一个图片"      android:src="@drawable/ic_launcher" />    <TextView      android:id="@+id/text"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="这是提示文字" />    <Button android:id="@+id/btn"         android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="这是按钮"/></FrameLayout>
    这里需要注意的就是之前使用的LinearLayout,现在需要改为FrameLayout,也就是我们使用的框架布局管理器。其中放置三个组件,这都是我们很熟悉的内容了,不用过多解释,下面直接运行程序来看一下效果:
http://dl.iteye.com/upload/attachment/0074/7777/8f81d100-4a57-3194-b03d-44665a08095a.jpg
    正如我们之前所说的,所有组件均在左上角叠加显示了。
    和LinearLayout类似,要在程序中控制FrameLayout就会涉及到FrameLayout类和FrameLayout.LayoutParams类,我们就可以通过这两个类来对框架布局管理器进行控制了。下面还是先来看一下FrameLayout.LayoutParams的文档:
http://dl.iteye.com/upload/attachment/0074/7781/4c49b7af-effe-3333-905e-3893c35d33c4.jpg
    这也是一个静态类,其继承结构为:
java.lang.Object
   ↳ android.view.ViewGroup.LayoutParams
    ↳ android.view.ViewGroup.MarginLayoutParams
   ↳ android.widget.FrameLayout.LayoutParams
    下面我们通过代码来控制FrameLayout:
这也是一个静态类,其继承结构为:java.lang.Object   ↳ android.view.ViewGroup.LayoutParams    ↳ android.view.ViewGroup.MarginLayoutParams   ↳ android.widget.FrameLayout.LayoutParams下面我们通过代码来控制FrameLayout:
    可以看出,这和LinearLayout的操作方式是类似的。下面来执行一下程序:
http://dl.iteye.com/upload/attachment/0074/7783/f0b781a1-1576-31bc-8b5d-de1b0967e6ab.jpg
    可以看到,这和我们使用XML布局文件的显示效果是一致的。
    本部分代码请参考附件
    接下文
页: [1]
查看完整版本: Android学习笔记12:框架布局管理器FrameLayout