2011-06-29 234 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>`