Android FrameLayout 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25679369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does FrameLayout do?
提问by Amin Ghasemi
I'm new to programming. I was using Graphical Layout then when I was reading xml file, I saw FrameLayout. Then I searched, but I couldn't find something useful. What is FrameLayout and what does it do?
我是编程新手。我正在使用图形布局,然后当我阅读 xml 文件时,我看到了 FrameLayout。然后我搜索,但我找不到有用的东西。什么是 FrameLayout,它有什么作用?
回答by Ojonugwa Jude Ochalifu
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 将子视图堆叠在彼此的顶部,最近的子视图位于堆叠顶部。在下面的示例中,TextView 是最新的,因此它会自动放置在 ImageView 的顶部。
For example:
例如:
<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>
Output:
输出:
回答by yoAlex5
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:
FrameLayout
是最简单的实现ViewGroup
。子视图绘制在堆栈中,最新添加的视图绘制在顶部。通常您可以使用以下方法之一或将它们结合起来:
- Add a single view hierarchy into
FrameLayout
- Add multiple children and use
android:layout_gravity
to navigate them
- 将单个视图层次结构添加到
FrameLayout
- 添加多个子项并用于
android:layout_gravity
导航它们
Another popular approaches of using FrameLayout
:
另一种流行的使用方法FrameLayout
:
- as a
Fragment
container - as an ancestor of your custom
ViewGroup
- 作为
Fragment
容器 - 作为你习俗的祖先
ViewGroup
回答by Asif Mushtaq
You can consider the word frame
as regular photo frame. What you do with that frame? you can place photos in that frame by one top to another. Same as in FrameLayout
we can place views ( Any layout, or widget like button, text, image so on) top of other as @ojonugwashows you the textview top of the Image.
您可以将这个词frame
视为常规相框。你用那个框架做什么?您可以将照片从一个顶部到另一个放置在该框架中。与FrameLayout
我们可以将视图(任何布局,或按钮、文本、图像等小部件)放在其他顶部一样,@ojonugwa向您显示图像的文本视图顶部。
回答by Metehan
Are you sure that you googled it?
你确定你用谷歌搜索过吗?
Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
Frame Layout 用于在屏幕上遮挡一个区域以显示单个项目。通常,FrameLayout 应该用于保存单个子视图,因为很难以可缩放到不同屏幕尺寸的方式组织子视图而不会使子视图相互重叠。
但是,您可以使用 android:layout_gravity 属性将多个子项添加到 FrameLayout 并通过为每个子项分配重力来控制它们在 FrameLayout 中的位置。
The secret of FrameLayout is how it layouts its children. Although normally designed to contain one item, it will happily stack up other element on top of each other. Thus FrameLayout is essentially a way to manipulate the Z-order of views on the screen.
This is super useful for a couple of UI tricks from HUD-like elements to sliding panels to more complex animated transitions. In this post we will see an example for each of those.
FrameLayout 的秘密在于它如何布局其子项。虽然通常设计为包含一个项目,但它会很高兴地将其他元素堆叠在一起。因此 FrameLayout 本质上是一种操纵屏幕上视图 Z 顺序的方法。
这对于从类似 HUD 的元素到滑动面板再到更复杂的动画过渡的几个 UI 技巧非常有用。在这篇文章中,我们将看到每个示例。
FrameLayout is designed to display a single item at a time. You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. I have created a simple XML layout using FrameLayout that shows how this works.
FrameLayout 旨在一次显示一个项目。一个 FrameLayout 中可以有多个元素,但每个元素都将根据屏幕的左上角定位。重叠的元素将重叠显示。我使用 FrameLayout 创建了一个简单的 XML 布局,展示了它是如何工作的。
回答by KOTIOS
Basically it puts one view on top of another for example :
基本上它将一个视图放在另一个视图之上,例如:
Inflating text on Image
在图像上膨胀文本
<FrameLayout>
<ImageView>
<Textview>
</FrameLayout>