Android 约束视图问题

Android 约束视图问题,android,android-studio,android-layout,Android,Android Studio,Android Layout,我试图使OTP登录屏幕,遇到了一些问题 下面是我的XML代码。我在这个活动中使用了ConstraintLayout和RelativeLayout。我还没有编写任何java代码,但是也定义了setContentView() 如果仔细观察我的输出,您可以看到左上角的白色矩形 我认为问题在于XML,我可能做错了什么,请帮助 您能告诉我如何修复吗?您可以尝试以下代码 <?xml version="1.0" encoding="utf-8"?> <androidx.constrai

我试图使OTP登录屏幕,遇到了一些问题

下面是我的XML代码。我在这个活动中使用了
ConstraintLayout
RelativeLayout
。我还没有编写任何java代码,但是也定义了
setContentView()

如果仔细观察我的输出,您可以看到左上角的白色矩形

我认为问题在于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">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="6dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <EditText
                android:id="@+id/editText"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:ems="3"
                android:hint="+91"
                android:inputType="number"
                android:textSize="12sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:ems="10"
                android:inputType="phone"
                android:text="1234567890"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/editText"
                app:layout_constraintTop_toTopOf="parent" />

            <com.google.android.material.button.MaterialButton
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                android:background="@android:color/holo_blue_light"
                android:text="@string/submit"
                android:textColor="#3C3F41"
                app:backgroundTint="#03A9F4"
                app:cornerRadius="24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/editText" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="Enter your mobile number, we will send you otp"
        app:layout_constraintBottom_toTopOf="@+id/cardView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@android:drawable/sym_def_app_icon" />
</androidx.constraintlayout.widget.ConstraintLayout>

将材质依赖项添加到项目中,并替换视图的图像和ID