安卓布局 align bottom

11
我在我的应用程序中有以下布局:
  <?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>

我需要将最后一个按钮放在页面底部。下面的图片展示了我需要的效果(左边是我现在有的,右边是我需要的)。

说明

谢谢。


3
考虑使用RelativeLayout,然后在您的按钮上使用android:layout_alignParentBottom="true" - james
你真的想把按钮放在滚动视图里面吗? - Matthew
是的,我需要<Button/><ScrollView/>内部。 - skywa1ker
4个回答

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中可用的空间,并将您的按钮推到底部。 编辑 - 如果您还希望按钮随着滚动而移动,则应遵循Romain Guy的文章,使用fillViewPort并在其中一个LinearLayout子元素上设置layout_weight

1
但我希望<Button/><ScrollView/>内部。 - skywa1ker
如果你正在使用碎片(Fragments),不要忘记在你的碎片布局中将layout_height设置为fill_parent。 - Cremons

7

3
你可以将ScrollView和按钮放在RelativeLayout中,并为按钮设置android:layout_alignParentBottom。

1

这是一个常见的问题,但很容易解决:

1)使用RelativeLayout 2)在其中,有两个视图或布局。 第一个对齐到顶部,并且具有与底部一个相等的底边距。 第二个对齐到底部

完成!希望这可以帮助到您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接