2011-03-10 103 views
10

我有以下佈局:Android的佈局在我的應用程序底部對齊

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scrollbars="vertical" 
    android:background="@drawable/bbg"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <TextView 
      android:id="@+id/Title" 
      android:text="@string/order_status" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:textSize="26sp" 
      android:textColor="#000000" 
      android:shadowColor="#FFFFFF" 
      android:shadowDx="0.5" 
      android:shadowDy="0.5" 
      android:shadowRadius="1.0"/> 
      <LinearLayout 
       android:orientation="vertical" 
       android:id="@+id/MainStatusWindow" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_margin="10sp" 
       android:background="@drawable/rounded_corners"> 
       <TextView 
       android:id="@+id/RateInfo" 
       android:text="" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="16sp" 
       android:layout_margin="10sp" 
       android:textColor="#5C5C5C"/> 
       <ImageView 
       android:src="@drawable/line" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:scaleType="fitXY"/> 
       <TextView 
       android:id="@+id/OrderStatus" 
       android:text="@string/please_wait" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_horizontal" 
       android:textSize="20sp" 
       android:layout_margin="10sp" 
       android:textColor="#222222"/> 
       <ImageView 
       android:src="@drawable/line" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:scaleType="fitXY"/> 
       <TextView 
       android:id="@+id/TimerStatus" 
       android:text="0:00" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center_horizontal" 
       android:textSize="50sp" 
       android:layout_margin="10sp" 
       android:textColor="#222222"/> 
      </LinearLayout> 
      <Button android:id="@+id/Cancel" 
       android:layout_marginTop="10sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/cancel_order" 
       android:textSize="18sp" 
       android:textColor="#111111" 
       android:layout_alignParentBottom="true" 
       android:background="@drawable/button_red"/> 
    </LinearLayout> 
    </ScrollView> 

我要最後一個按鈕是在頁面的底部。 下圖顯示了我需要的東西(左邊是我所需要的,右邊是我需要的東西)。

Explanation

謝謝。

+3

考慮使用'RelativeLayout'然後在你的按鈕上使用'android:layout_alignParentBottom =「true」' – binnyb 2011-03-10 14:21:13

+0

你真的想讓你的按鈕*在scrollview中嗎? – 2011-03-10 15:52:07

+0

是的,我需要'

回答

28

總體佈局的組織應該是:

<LinearLayout orientation="vertical"> 
    <ScrollView android:layout_weight="1" 
     android:width="fill_parent" 
     android:height="wrap_content"> 
     ... linear layout with your TextViews ... 
    </ScrollView> 
    <Button/> 
</LinearLayout> 

layout_weight="1"屬性使這一觀點,填補了外LinearLayout中的可用空間,並會推動你的按鈕底部。

編輯 - 如果你想要的按鈕以及滾動,那麼你應該遵循羅曼蓋伊的article,使用fillViewPort並在您LinearLayout孩子中的一個設置layout_weight

+1

但是我想'

+0

謝謝你這篇文章。它有助於! – skywa1ker 2011-03-10 17:10:51

+0

如果您正在處理碎片,請不要忘記在碎片佈局中將layout_height設置爲fill_parent。 – Cremons 2013-04-13 13:37:17

3

您可以使用把你的滾動型和RelativeLayout的內部的按鈕,並設置機器人:layout_alignParentBottom的按鈕。

1

這是一個常見的問題,但容易解決:

1)使用的RelativeLayout 2)在那裏,有兩個視圖或佈局。 第一個對齊頂部,底部邊距等於底部邊緣的高度。 第二個對齊底部

完成!希望這可以幫助。