如何在 UI 底部的固定位置设置按钮?
Posted
技术标签:
【中文标题】如何在 UI 底部的固定位置设置按钮?【英文标题】:how to set a button at a fixed location on the bottom of UI? 【发布时间】:2011-09-25 01:11:07 【问题描述】:我希望一个按钮始终出现在固定位置,在 UI 的页脚中 ؟ (总是,如果它上面有组件)
【问题讨论】:
来吧,把 XML 粘贴到这里(如果它太大,请在 gist.github.com 上) 相同的***.com/questions/14779688 和***.com/questions/2386866 【参考方案1】:请在主布局下采用一种相对布局。将其高度和宽度设置为填充父级,并将其重力设置为底部,并将任何文本视图或任何您想要的按钮放入其中。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<RelativeLayout
android:layout_
android:layout_
android:gravity="bottom">
<Button
android:layout_
android:layout_
android:text="Bottom Gravity" />
</RelativeLayout>
<LinearLayout
android:layout_
android:layout_>
<Button
android:layout_
android:layout_
android:text="Without Gravity" />
</LinearLayout>
</FrameLayout>
【讨论】:
完美答案。作品。但应该使用“match_parent”而不是“fill_parent” 它工作得很好,但是是否有任何线索表明按钮没有被键盘提升? 非常感谢。它对我有用。我有Linearlayout的嵌套。其中一个我更改为具有您指定的属性和值的RelativeLayout,并且效果很好。【参考方案2】:这取决于您使用的布局。
在 RelativeLayout 上有
android:layout_alignParentBottom="true"
在 LinearLayout 上,将其放在底部并确保正确设置元素 layout_weight。
另外,检查属性
android:layout_gravity
并注意到它不同于
android:gravity
【讨论】:
我确认你的回答,他应该使用 relativeLayout 来修复底部的按钮android:layout_alignParentBottom="true"
,我想补充一点,如果他想将他的按钮保持在前面,在他的他可以调用该方法的代码:btn.bringToFront()
【参考方案3】:
设置android:layout_gravity="bottom"
。希望这会有所帮助。
【讨论】:
【参考方案4】:放
android:layout_alignParentBottom="true"
在你的相对布局中。
【讨论】:
这个答案可以通过注意格式来改进。【参考方案5】:如果有人有两个按钮,你可以为我做这个
<RelativeLayout
android:layout_
android:layout_
android:gravity="bottom">
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_
android:gravity="bottom" >
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_yes"
android:layout_
android:layout_
android:padding="12dp"
android:text="Valider"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_no"
android:layout_
android:layout_
android:padding="12dp"
android:text="Annuler"/>
</LinearLayout>
</RelativeLayout>`
【讨论】:
以上是关于如何在 UI 底部的固定位置设置按钮?的主要内容,如果未能解决你的问题,请参考以下文章