Java Android 浮动视图(在其他视图之上)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2268569/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:28:41  来源:igfitidea点击:

Android floating view (over other views)

javaandroidmobileandroid-layout

提问by dave paola

I've been messing around with this for a few days now, hopefully someone here can lend me a hand.

我已经被这个问题困扰了几天了,希望这里有人能帮我一把。

I have a simple two-column layout, the left side is a navigation bar with buttons, the right side is a content panel. When the user taps one of the buttons (say, the third one down), I'd like to have a floating view aligned to the right of this button but floating on top of the content pane. Here's a picture to illustrate what I mean: Layout

我有一个简单的两列布局,左侧是带按钮的导航栏,右侧是内容面板。当用户点击其中一个按钮(比如第三个向下)时,我希望有一个浮动视图与此按钮的右侧对齐,但浮动在内容窗格的顶部。这是一张图片来说明我的意思: 布局

Everything I've tried shoves the floating menu inside the navigation bar or inside the content panel, which is not what I want. Any ideas? Here's basically what I have so far:

我尝试过的所有操作都将浮动菜单推到导航栏内或内容面板内,这不是我想要的。有任何想法吗?到目前为止,这基本上是我所拥有的:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_alignParentLeft="true"
        android:id="@+id/navigation_bar"
    >
        <FrameLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.14"
        >
            <ImageButton 
                android:id="@+id/button1_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/icon"
                android:layout_gravity="center"
            />
        </FrameLayout>
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.14"
        >
            <ImageButton 
                android:id="@+id/button2_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/icon"
                android:layout_gravity="center"
            />
        </FrameLayout>
    </LinearLayout>
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.14"
        android:layout_toRightOf="@id/navigation_bar"
    >
    </FrameLayout>
</RelativeLayout>

采纳答案by Cheryl Simon

A FrameLayout allows you to have a view overlapping another view. I'm not sure it makes sense to have them with only one child view, as you have in your example. Try having a FrameLayout at the highest level, with your "static" view as the first child element, and the floating menu as the second child.

FrameLayout 允许您让一个视图与另一个视图重叠。我不确定让它们只有一个子视图是否有意义,就像您在示例中所做的那样。尝试在最高级别使用 FrameLayout,将“静态”视图作为第一个子元素,将浮动菜单作为第二个子元素。

The developer documentshave a good overview the layout types, it might help you get started.

开发者文档有一个很好的概述的布局类型,它可以帮助你上手。

回答by Shachar

RelativeLayoutis what you want.
FrameLayoutmust only have one child, and is therefore, usually, only used for a placeholder where other layouts come later (such as the main frame of the activity).
AbsoluteLayoutis deprecated, and should not be used.

RelativeLayout是你想要的。
FrameLayout必须只有一个孩子,因此,通常仅用于其他布局稍后出现的占位符(例如活动的主框架)。
AbsoluteLayout已弃用,不应使用。

RelativeLayoutallows the views to overlap, and allows doing, pretty much, anything you want.

RelativeLayout允许视图重叠,并允许做任何你想做的事情。