【问题标题】:how to set a button at a fixed location on the bottom of UI?如何在 UI 底部的固定位置设置按钮?
【发布时间】:2011-09-25 01:11:07
【问题描述】:

我希望一个按钮始终出现在固定位置,在 UI 的页脚中 ؟ (总是,如果它上面有组件)

【问题讨论】:

标签: android


【解决方案1】:

请在主布局下采用一种相对布局。将其高度和宽度设置为填充父级,并将其重力设置为底部,并将任何文本视图或任何您想要的按钮放入其中。

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

【讨论】:

  • 完美答案。作品。但应该使用“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_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>`
    

    【讨论】:

      猜你喜欢
      • 2015-09-22
      • 1970-01-01
      • 2017-04-22
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2016-12-01
      相关资源
      最近更新 更多