1
2

More than 3 years have passed since last update.

スクロール可能な画面でViewを下に配置する

Posted at

はじめに

スクロール可能な画面で下に配置されているViewを、画面に余裕があれば、画面いっぱいに使ってViewを配置するようなレイアウトの実装方法。

実装方法

<ScrollView
    ...
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            ...
            android:id="@+id/text_view"
            app:layout_constraintBottom_toTopOf="@id/button"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="spread_inside"
            />

        <Button
            ...
            android:id="@+id/button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/text_view"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

fillViewport

fillViewportを設定することで、ScrollView内のレイアウトを拡大して画面いっぱいに表示できる。

<ScrollView
    ...
    android:fillViewport="true"
    />

spread_inside

下に配置するViewと上のViewにChainStyleを指定することで、Viewを画面端に寄せられる。
具体的には、上のViewのlayout_constraintBottom_toTopOfに下のView、layout_constraintVertical_chainStylespread_insideを設定する。
また、下のViewのlayout_constraintTop_toBottomOfに上のViewを設定する。

        <TextView
            ...
            android:id="@+id/text_view"
            app:layout_constraintBottom_toTopOf="@id/button"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="spread_inside"
            />

        <Button
            ...
            android:id="@+id/button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/text_view"
            />
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2