NestedScrollViewの孫要素にレイアウトの残りを全て埋めてもらおうとしたら、思いの外ハマってしまったので備忘録として。
経緯
・NestedScrollViewで要素を囲んでいる既存のViewに、CustomViewを追加し、残っていた隙間を埋めたかった。
スタートとゴール
(あくまでイメージなので適当なViewで構成しています。)
こんなソース。
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#33409577"
android:text="元々あった要素1" />
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#336ADF2A"
android:text="元々あった要素2" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
それをこうしたかった。
とりあえずやってみた
(ここもCustomViewではなく適当なTextViewで構成しています。)
まあ、追加してandroid:layout_height="match_parent"を設定するだけかなあと考えておりました。
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#33409577"
android:text="元々あった要素1" />
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#336ADF2A"
android:text="元々あった要素2" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:background="#3310BCBB"
android:text="カスタムビュー" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
これを実行すると以下になった。
そんな・・・。
色々調べた結果
NestedScrollViewに
android:fillViewport="true"
を追加すれば正しく表示された。
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#33409577"
android:text="元々あった要素1" />
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#336ADF2A"
android:text="元々あった要素2" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:background="#3310BCBB"
android:text="カスタムビュー" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
NestedScrollViewの中身がそれ自体より小さいときに埋める(=fill)かどうかという設定。
大変初歩的な部分でした・・・。
ちなみにScrollViewでも同様。
中身をNestedScrollView自体のサイズに合わせたい場合にはtrueにしましょう。
大変勉強になりました。