【发布时间】:2021-08-18 17:57:32
【问题描述】:
我在 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>
【问题讨论】:
-
即使高度指定为 match_parent,布局也可能超出其边界。在 ConstraintLayout 中,每个视图的高度(通常是
wrap_content+ 上边距 + 下边距可能会大于屏幕上可能的高度。在这种情况下,每个布局都会超出边界。有有几种方法可以解决这个问题。按照@che10 的建议添加一个滚动视图,缩小垂直边距或将所有视图放在一个垂直视图链中,该垂直视图链被限制在容器的顶部和底部。 -
感谢您的评论@Cheticamp
标签: android android-fragments android-constraintlayout android-framelayout