首页 > 解决方案 > Android:片段不适合片段容器

问题描述

我在 MainActivity 中有一个片段容器视图。我有一个寄存器片段。在寄存器片段内,我有约束布局。当我在手机中启动应用程序时,寄存器片段不适合片段容器。我怎么解决这个问题?


我的应用程序的屏幕截图


我的 MainActivity.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/app_name"
        android:textSize="64dp"
        android:fontFamily="@font/roboto_medium"
        android:textColor="@color/siyah"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/fragmentcontainerview_tasarimi"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:layout_margin="22dp"
        app:navGraph="@navigation/nav_graph"
        android:elevation="20dp"
        android:stateListAnimator="@null"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我的注册片段 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".RegisterEkrani"
    android:background="@drawable/arkaplatform"
    android:fitsSystemWindows="true">

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

        <TextView
            android:id="@+id/registerekrani_main_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="30dp"
            android:fontFamily="@font/roboto_medium"
            android:text="Register"
            android:textColor="@color/siyah"
            android:textSize="30dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_isim_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="İsmin"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_main_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_username_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="Kullanıcı Adın"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:helperTextTextColor="@color/siyah"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_isim_edittext">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_mail_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="Mail adresin"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_username_edittext">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"/>

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_sifren_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="Şifren"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:helperTextTextColor="@color/siyah"
            app:endIconMode="password_toggle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_mail_edittext">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"/>

        </com.google.android.material.textfield.TextInputLayout>


        <Button
            android:id="@+id/register_button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@drawable/buton_tasarim"
            android:elevation="10dp"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:stateListAnimator="@null"
            android:text="Register"
            android:layout_marginTop="40dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/register_iptal"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_sifren_edittext" />


        <TextView
            android:id="@+id/register_iptal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/roboto_medium"
            android:gravity="center"
            android:letterSpacing="0.10"
            android:padding="10dp"
            android:text="Geri"
            android:textColor="@color/siyah"
            android:textSize="15dp"
            android:layout_marginTop="45dp"
            app:layout_constraintEnd_toStartOf="@id/register_button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_sifren_edittext" />



    </androidx.constraintlayout.widget.ConstraintLayout>


</FrameLayout>

标签: androidandroid-fragmentsandroid-constraintlayoutandroid-framelayout

解决方案


注册片段非常适合,它根据片段容器中可用的区域进行拟合。由于该区域是有限的,所以这就是为什么对你来说它似乎被切断了。

这是您可以执行的操作,您可以在 RegisterFragment 中添加 ScrollView,我不知道您为什么使用 FrameLayout,但据我所知,您可以用 ScrollView 替换它。除此之外,您需要为最后一个元素提供底部约束和底部的边距。这就是它的样子。

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RegisterEkrani"
    android:background="@drawable/arkaplatform"
    >

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

        <TextView
            android:id="@+id/registerekrani_main_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="30dp"
            android:fontFamily="@font/roboto_medium"
            android:text="Register"
            android:textColor="@color/siyah"
            android:textSize="30dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_isim_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="İsmin"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_main_text">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_username_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="Kullanıcı Adın"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:helperTextTextColor="@color/siyah"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_isim_edittext">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_mail_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="Mail adresin"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_username_edittext">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"/>

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/registerekrani_sifren_edittext"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="22dp"
            android:hint="Şifren"
            android:textColorHint="@color/turuncu"
            app:boxStrokeColor="@color/turuncu"
            app:helperTextTextColor="@color/siyah"
            app:endIconMode="password_toggle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_mail_edittext">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"/>

        </com.google.android.material.textfield.TextInputLayout>


        <Button
            android:id="@+id/register_button"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@drawable/buton_tasarim"
            android:elevation="10dp"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:stateListAnimator="@null"
            android:text="Register"
            android:layout_marginTop="40dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/register_iptal"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_sifren_edittext" />


        <TextView
            android:id="@+id/register_iptal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/roboto_medium"
            android:gravity="center"
            android:letterSpacing="0.10"
            android:padding="10dp"
            android:text="Geri"
            android:textColor="@color/siyah"
            android:textSize="15dp"
            android:layout_marginTop="45dp"
            android:layout_marginBottom="24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/register_button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/registerekrani_sifren_edittext" />



    </androidx.constraintlayout.widget.ConstraintLayout>


</ScrollView>

推荐阅读