Как сделать прокручеваемый Fragment
Мне надо чтобы Fragment растягивался до размеров экрана, а если в него что-то не влезло, его можно было прокрутить. Я попробовал сделать так.
Activity в которую вставляется фрагмент.
<LinearLayout 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_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".components.login.ui.LogInActivity"
android:id="@+id/log_in_activity"
android:screenOrientation="portrait">
</LinearLayout>
Сам фрагмент
<ScrollView
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"
tools:context=".components.login.ui.SignUpFirstTypeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:paddingBottom="30dp"
android:paddingTop="30dp"
android:paddingRight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_bitmap"
android:paddingLeft="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:id="@+id/logo_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/sign_up_types_account"
app:layout_constraintVertical_chainStyle="spread_inside"
android:contentDescription="@string/logo_description"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sign_up_types_account"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/logo_image"
app:layout_constraintBottom_toTopOf="@id/sign_up_buttons">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f50707"
android:textSize="15sp"
android:visibility="gone"
android:id="@+id/login_email_alert"/>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/your_email"
android:background="@drawable/border_style"
android:padding="12dp"
android:layout_marginBottom="20dp"
android:id="@+id/login_email"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f50707"
android:textSize="15sp"
android:visibility="gone"
android:id="@+id/login_password_alert"
android:inputType="textEmailAddress"/>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/your_password"
android:background="@drawable/border_style"
android:padding="12dp"
android:inputType="textPassword"
android:id="@+id/login_password"
app:passwordToggleEnabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/sign_up_buttons"
app:layout_constraintTop_toBottomOf="@id/sign_up_types_account"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/log_in"
android:backgroundTint="#FFB803"
android:textStyle="bold"
android:textColor="#000"
android:textSize="18sp"
android:id="@+id/button_login"
android:layout_marginBottom="20dp"/>
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop"
android:src="@drawable/back_arrow"
android:background="@android:color/transparent"
android:id="@+id/login_back_arrow"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Но на выходе у меня получается вот-это
Почему? Что я сделал не так?
P.S затем это будет регистрация, и полей ввода будет больше. Поэтому нужен скролл. Заранее спасибо!
Ответы (1 шт):
Ваша проблема в том, что ScrollView сам по себе не хочет занимать весь доступный объем. Чтобы это случилось надо ему поставить атрибут android:fillViewport="true"
Ну и как вам уже сказали в комментариях - не используйте ConstraintLayout для умещения переменного числа объектов. Внутри ScrollView редко нужно что-то, кроме LinearLayout, в вашем случае точно нужно использовать его