画面下のビューを揃えるにはどうすればいいですか?

これが私のレイアウトコードです。

<?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>
ソリューション

これを行う現代的な方法は、ConstraintLayoutを用意し、ビューの底面をConstraintLayoutの底面に拘束するというものです。

以下の例では、画面の端と底に合わせて配置されるFloatingActionButtonを作成しています。





参考までに、私の古い回答を残しておきます。

ConstraintLayoutが導入される前の回答は、relative layoutでした。


画面全体を埋めるような相対レイアウトであれば、android:layout_alignParentBottomを使ってボタンを画面の下に移動させることができるはずです。

下部のビューが相対レイアウトで表示されていない場合は、その上のレイアウトがすべてのスペースを占めているのかもしれません。このような場合は、一番下にあるべきビューをレイアウトファイルの最初に置き、残りのレイアウトを android:layout_above でビューの上に配置します。これにより、一番下のビューが必要なだけのスペースを取ることができ、残りのレイアウトは画面の残りすべてを埋めることができます。

解説 (6)

ScrollViewでは、RelativeLayoutがページ下部のScrollViewにあるものと重なってしまうため、これは機能しません。

私は、動的に伸張する FrameLayout を使用してこれを修正しました。















解説 (4)

2つ目の relative レイアウトを1つ目のレイアウトの中に入れ子にする必要もありません。単に、ButtonEditTextandroid:layout_alignParentBottom="true" を使用するだけです。

解説 (1)