What does FrameLayout do?

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

74730 次浏览

基本上,它把一个视图放在另一个视图之上,例如:

在图像上放大文本

  <FrameLayout>


<ImageView>
<Textview>


</FrameLayout>

你确定你谷歌过了吗?

帧布局的设计是为了在屏幕上屏蔽一个区域来显示一个单独的项目。通常,FrameLayout 应该用于保存单个子视图,因为很难以可伸缩到不同屏幕大小的方式来组织子视图,而子视图又不会彼此重叠。

但是,您可以向 FrameLayout 和控件添加多个子级 它们在框架布局中的位置 子元素,使用 android: lay_ vity 属性。

FrameLayout 的秘密在于它如何布局它的子元素 元素,它会很高兴地将其他元素叠加在一起。 因此 FrameLayout 本质上是一种操作 屏幕上的视图。

这对于来自 HUD 类元素的几个 UI 技巧来说非常有用 to sliding panels to more complex animated transitions. In this post 我们将看到每一个例子。

FrameLayout 旨在一次显示一个项 在 FrameLayout 中有多个元素,但是每个元素都是 基于屏幕左上角定位。重叠的元素 will be displayed overlapping. I have created a simple XML layout using FrameLayout that shows how this works.

You use a FrameLayout to stack child views on top of each other, with the most recent child on top of the stack. In the example below, the TextView is the most recent, so it is automatically placed on top of the 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

你可以把 frame这个词看作是普通的相框。你拿那个相框做什么?你可以把照片放在那个相框里,从一个顶端放到另一个顶端。和在 FrameLayout中一样,我们可以将视图(任何布局,或小部件,如按钮、文本、图像等)放在其他视图的顶部,因为 @ ojonugwa显示了图像的文本视图顶部。

FrameLayout is the simplest implementation of ViewGroup. Child views are drawn are in a stack, where the latest added view is drawn at the top. Usually you can use one of the next approaches or combine them:

  1. 将单个视图层次结构添加到 FrameLayout
  2. 添加多个子级并使用 android:layout_gravity导航它们

enter image description here

使用 FrameLayout的另一种流行方法是:

  • 作为 Fragment容器
  • 作为您的定制 ViewGroup的祖先