FrameLayout做什么?

时间:2014-09-05 05:49:00

标签: android android-framelayout

我是编程新手。我正在使用Graphical Layout然后当我阅读xml文件时,我看到了FrameLayout。然后我搜索了,但我找不到有用的东西。什么是FrameLayout以及它做了什么?

5 个答案:

答案 0 :(得分:106)

使用FrameLayout将子视图堆叠在一起,最近的子视图位于堆栈顶部。在下面的示例中,TextView是最新的,因此它会自动放在ImageView的顶部。

例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:src="@drawable/bitmapie" />

    <TextView
        android:id="@+id/descTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginTop="70dp"
        android:background="@android:color/holo_blue_light"
        android:padding="10dp"
        android:text="TextView placed at the top of the Imageview"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

</FrameLayout>

输出:

enter image description here

答案 1 :(得分:22)

FrameLayout旨在屏蔽屏幕上的某个区域以显示单个项目。通常,FrameLayout应该用于保存单个子视图,因为以可扩展到不同屏幕大小而儿童不会相互重叠的方式组织子视图可能很困难。但是,您可以使用FrameLayout属性为FrameLayout添加多个孩子,并通过为每个孩子分配重力来控制他们在android:layout_gravity中的位置。

子视图以堆栈形式绘制,最近添加的子项位于顶部。

enter image description here

使用FrameLayout的另一种流行方法是Fragment容器

了解更多here

答案 2 :(得分:20)

你确定用Google搜索了吗?

  

帧布局旨在阻挡屏幕上的区域以显示单个项目。通常,FrameLayout应该用于保存单个子视图,因为很难以可扩展到不同屏幕大小的方式组织子视图而不会让孩子彼此重叠。

     

但是,您可以将多个子项添加到FrameLayout和控件中   它们在FrameLayout中的位置,通过为每个分配重力   child,使用android:layout_gravity属性。

  

FrameLayout的秘诀在于它如何布置其子项。虽然通常设计包含一个   item,它会愉快地将其他元素叠加在一起。   因此,FrameLayout本质上是一种操纵Z阶的方法   在屏幕上观看。

     

这对于来自HUD类元素的几个UI技巧非常有用   滑动面板到更复杂的动画过渡。在这篇文章中   我们将看到每个例子。

  

FrameLayout旨在一次显示一个项目。您可以   在FrameLayout中有多个元素,但每个元素都是   基于屏幕的左上角定位。重叠的元素   将显示重叠。我创建了一个简单的XML布局   使用FrameLayout来显示它是如何工作的。

答案 3 :(得分:16)

您可以将单词frame视为常规相框。你用那个框架做什么?你可以将照片放在那个框架中。与FrameLayout相同,我们可以将视图(任何布局或按钮,文本,图像等小部件)置于其他顶部,因为 @ojonugwa 会向您显示图像的textview顶部。 / p>

答案 4 :(得分:6)

基本上它将一个视图置于另一个视图之上,例如:

在图像上充气文字

  <FrameLayout>

<ImageView>
<Textview>

  </FrameLayout>