2011-03-10 108 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)在那里,有两个视图或布局。 第一个对齐顶部,底部边距等于底部边缘的高度。 第二个对齐底部

完成!希望这可以帮助。