如何在屏幕底部对齐视图?
这是我的布局代码;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="@string/welcome" 
        android:id="@+id/TextView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
    </TextView>
    <LinearLayout android:id="@+id/LinearLayout" 
        android:orientation="horizontal"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="bottom">
            <EditText android:id="@+id/EditText" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content">
            </EditText>
            <Button android:text="@string/label_submit_button" 
                android:id="@+id/Button" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </Button>
    </LinearLayout>
</LinearLayout>
这看起来像是在左边,我想要它看起来像在右边。

显而易见的答案是将TextView设置为fill_parent的高度,但这不会让按钮或输入字段留下空间。 基本上问题是,我希望提交按钮和文本条目在底部是固定高度,文本视图用于填充空间的其余部分,类似于水平线性布局,我希望提交按钮可以包装其内容并为文本条目填充剩余的空间。
如果线性布局中的第一个项目被告知fill_parent,它就是这么做的,没有留下其他项目的空间,我怎样才能得到一个线性布局中第一个填充所有空间的项目,除了剩下的布局中的项目?
编辑:
相对布局确实是答案 - 谢谢!
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <TextView 
        android:text="@string/welcome" 
        android:id="@+id/TextView"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
    </TextView>
    <RelativeLayout 
        android:id="@+id/InnerRelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
        <Button 
            android:text="@string/label_submit_button" 
            android:id="@+id/Button"
            android:layout_alignParentRight="true" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </Button>
        <EditText 
            android:id="@+id/EditText" 
            android:layout_width="fill_parent"
            android:layout_toLeftOf="@id/Button"
            android:layout_height="wrap_content">
        </EditText>
    </RelativeLayout>
</RelativeLayout>
  我认为你应该尝试一个相对的布局。 
  如果你有一个填充整个屏幕的相对布局,你应该可以使用android:layout_alignParentBottom将按钮移动到屏幕的底部。 
  如果底部的视图没有以相对布局显示,那么可能它上面的布局占用了所有空间。  在这种情况下,您可以将视图放置在底部,首先放置在布局文件中,然后使用android:layout_above将视图的其余部分放置在视图上方。  这使得底部视图可以占用尽可能多的空间,其余的布局可以填充屏幕的其余部分。 
  在ScrollView这不起作用,因为RelativeLayout会重叠页面底部的ScrollView 。 
  我使用动态拉伸的FrameLayout来修复它: 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" 
    android:layout_width="match_parent"
    android:fillViewport="true">
    <LinearLayout 
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical">
                <!-- content goes here -->
                <!-- stretching frame layout, using layout_weight -->
        <FrameLayout
            android:layout_width="match_parent" 
            android:layout_height="0dp"
            android:layout_weight="1">
        </FrameLayout>
                <!-- content fixated to the bottom of the screen -->
        <LinearLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal">
                                   <!-- your bottom content -->
        </LinearLayout>
    </LinearLayout>
</ScrollView>
您可以通过在线性布局中嵌套相对布局来保留初始线性布局:
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="welcome" 
        android:id="@+id/TextView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
    </TextView>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button android:text="submit" 
            android:id="@+id/Button" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true">
        </Button>
        <EditText android:id="@+id/EditText" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/Button"
            android:layout_alignParentBottom="true">
        </EditText>
    </RelativeLayout>
</LinearLayout>
上一篇: How to align views at the bottom of the screen?
下一篇: Strange out of memory issue while loading an image to a Bitmap object
