Android浮动按钮覆盖在视图之上

13

我试图在我的视图上放置一个浮动按钮,我进行了谷歌搜索,并找到了这个链接,该链接非常好地概括了它。

http://www.jondev.net/articles/Floating_Views_in_Android_(Buttons)

虽然针对单个按钮这是正确的,但如果我想要两个浮动按钮,一个位于“左上角”,另一个位于“右下角”,怎么办?

我考虑使用相对布局,其中包含两个具有不同布局重心的按钮。这样可以吗?我尝试了一下,但结果很糟糕。是否有其他人也尝试过这样做?如果有,请告诉我如何实现。


1
该链接显示“页面不存在”。 - superM
链接中不显示任何内容。 - KMI
@superM 你需要在链接中添加右括号)来闭合。 - Thommy
@Thebestshoot,请编辑您的问题并正确放置那个括号。 - superM
3个回答

22

你可以使用RelativeLayout来实现这个效果:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="15dp"
        android:text="Button" />
</RelativeLayout>

请注意,最后添加的小部件位于最上方。


2
你可以使用RelativeLayout来完成,虽然你也可以使用FrameLayout来实现(就像你链接中的示例一样)。 在FrameLayout中为按钮设置适当的重力(Gravity.BOTTOM|Gravity.RIGHT或通过XML...),在RelativeLayout中为按钮设置所需的规则:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"

etc.


我尝试使用FrameLayout,但未能使其正常工作,也许我在所做的事情上有误,请问是否可以详细说明一下。 - Thebestshoot

1
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="15dp"
        android:text="Button" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="15dp"
        android:text="Button" />
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接