2011-06-29 167 views
6

我想要一个按钮始终出现在固定的位置,在UI的页脚中? (总是,如果它有或没有的组件)如何在UI底部的固定位置设置按钮?

+0

发表您的XML这里 – ingsaurabh

+0

来吧,这里贴上XML(上gist.github.com如果过大),请 –

+0

相同http://stackoverflow.com/questions/14779688/和http://stackoverflow.com/questions/2386866 –

回答

22

请把你的主要布局在一个相对布局。将其高度和宽度设置为填充父级,并将其重力设置为底部,并将所需的任何文本视图或任何按钮放入其中。

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

enter image description here

+0

完美答案。作品。但应该使用“match_parent”而不是“fill_parent” – rjdthegreat

+0

这对我来说也很好 –

2

设置android:layout_gravity="bottom"。希望这可以帮助。

6

这取决于您使用的布局。

在一个RelativeLayout的有

android:layout_alignParentBottom="true" 

在一个LinearLayout中,将其放置在底部,并确保设置元素layout_weight正常。

此外,检查属性

android:layout_gravity 

,并注意到它的不同比

android:gravity 
+0

我确认你的答案,他应该使用一个relativeLayout来修复底部的按钮'android:layout_alignParentBottom = “true”,我想补充一点,如果他想让自己的按钮保持在前面,在他的代码中,他可以调用方法:'btn.bringToFront()' – Houcine

0

android:layout_alignParentBottom="true" 

在相对布局。

+1

注意格式化可以改善此答案。 – Thomas

0

,如果任何一个有两个按钮,你可以做这一点,兽人我

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