【发布时间】:2011-09-25 01:11:07
【问题描述】:
我希望一个按钮始终出现在固定位置,在 UI 的页脚中 ؟ (总是,如果它上面有组件)
【问题讨论】:
-
来吧,把 XML 粘贴到这里(如果它太大,请在 gist.github.com 上)
标签: android
我希望一个按钮始终出现在固定位置,在 UI 的页脚中 ؟ (总是,如果它上面有组件)
【问题讨论】:
标签: android
请在主布局下采用一种相对布局。将其高度和宽度设置为填充父级,并将其重力设置为底部,并将任何文本视图或任何您想要的按钮放入其中。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bottom Gravity" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Without Gravity" />
</LinearLayout>
</FrameLayout>
【讨论】:
这取决于您使用的布局。
在 RelativeLayout 上有
android:layout_alignParentBottom="true"
在 LinearLayout 上,将其放在底部并确保正确设置元素 layout_weight。
另外,检查属性
android:layout_gravity
并注意到它不同于
android:gravity
【讨论】:
android:layout_alignParentBottom="true",我想补充一点,如果他想将他的按钮保持在前面,在他的他可以调用该方法的代码:btn.bringToFront()
设置android:layout_gravity="bottom"。希望这会有所帮助。
【讨论】:
放
android:layout_alignParentBottom="true"
在你的相对布局中。
【讨论】:
如果有人有两个按钮,你可以为我做这个
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom" >
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="Valider"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="Annuler"/>
</LinearLayout>
</RelativeLayout>`
【讨论】: