视图上的 Android 浮动按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10551079/
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
Android floating buttons over a view
提问by Thebestshoot
I was trying to have a floating button on my view, I googled and found this link which pretty well sums it up.
我试图在我的视图上有一个浮动按钮,我用谷歌搜索并找到了这个链接,它很好地总结了它。
http://www.jondev.net/articles/Floating_Views_in_Android_(Buttons)
http://www.jondev.net/articles/Floating_Views_in_Android_(按钮)
While this is true for one button, but what if i want to have two floating buttons one at "top left" and another at "bottom right".
虽然这对于一个按钮是正确的,但是如果我想有两个浮动按钮,一个在“左上角”,另一个在“右下角”,该怎么办。
I thought of having a relative layout which has two buttons with lay out gravities different. Will this work. I tried it and miserably failed. Has any one else tried to do this? Is there any way of doing this, if so please let me know.
我想有一个相对布局,其中有两个按钮的布局重力不同。这会起作用吗。我试过了,惨遭失败。有没有其他人尝试过这样做?有没有办法做到这一点,如果有,请告诉我。
回答by Thommy
You can achive that with a RelativeLayout
:
您可以通过以下方式实现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>
Note that the last added Widget is on top.
请注意,最后添加的 Widget 位于顶部。
回答by avimak
You can do it with RelativeLayout
although you can also achieve that using FrameLayout
(like in the example in your the link).
In FrameLayout
Set a proper gravity to the the buttons (Gravity.BOTTOM|Gravity.RIGHT
, or via XML...), and in RelativeLayout
set the reuiqred rules to the Buttons:
您可以使用它RelativeLayout
来实现,尽管您也可以使用它来实现FrameLayout
(如链接中的示例)。在FrameLayout
为按钮设置适当的重力(Gravity.BOTTOM|Gravity.RIGHT
,或通过 XML ...),并将RelativeLayout
所需的规则设置为按钮:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
etc.
等等。
回答by IronBlossom
<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>