これが私のレイアウトコードです。
<?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を作成しています。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
参考までに、私の古い回答を残しておきます。
ConstraintLayoutが導入される前の回答は、relative layoutでした。
画面全体を埋めるような相対レイアウトであれば、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>
2つ目の relative
レイアウトを1つ目のレイアウトの中に入れ子にする必要もありません。単に、Button と EditText に android:layout_alignParentBottom="true"
を使用するだけです。