【问题标题】:Android floating view (over other views)Android 浮动视图(在其他视图之上)
【发布时间】:2010-02-15 20:02:51
【问题描述】:

我已经搞砸了几天了,希望这里有人可以帮我一把。

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

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

<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>

【问题讨论】:

  • +1 在绘图前:P
  • 戴夫,你有没有设法实现这一点。我需要做一些非常相似的事情,所以如果你能分享一些你的实现,我会很高兴。

标签: java android mobile android-layout


【解决方案1】:

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

developer documents 很好地概述了布局类型,它可能会帮助您入门。

【讨论】:

  • RelativeLayout 也支持重叠视图。
  • RelativeLayout 继承自 FrameLayout
  • 链接在 2017 年 4 月损坏,请改用 this
  • @Cheryl 链接已损坏
【解决方案2】:

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

RelativeLayout 允许视图重叠,并且几乎可以做任何你想做的事情。

【讨论】:

  • FrameLayout 绝对可以有多个孩子。
猜你喜欢
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多