【问题标题】:Why is the button compacted or off-screen in Android Studio?为什么 Android Studio 中的按钮被压缩或离屏?
【发布时间】:2026-02-16 18:55:01
【问题描述】:

你知道我有一个问题,为什么我有这样的“添加”按钮,它实际上就好像它正在离开活动,即使文本“添加数据......”也是错误的,因为这个,我不得不说我对来自 android studio 的所有这一切还是新手,所以任何东西都可以帮助我,我尝试了很多但我无法完全显示按钮,只有你在图像中看到的部分(我尝试使用线性布局)

it's done like that

this is how I would like it to look

我添加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:id="@+id/ConstraintLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_degradado"
    tools:context=".NuevoPacienteActivity">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="10dp"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="15dp"
        app:cardElevation="10dp"
        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:id="@+id/ConstraintLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:paddingBottom="16dp"
                android:text="@string/agregue_datos_del_paciente"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/textInputLayout8"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_chainStyle="packed" />

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout8"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:fadeScrollbars="false"
                android:importantForAutofill="no"
                app:errorEnabled="true"
                app:layout_constraintBottom_toTopOf="@+id/textInputLayout9"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView"
                tools:ignore="TextContrastCheck,TouchTargetSizeCheck">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editTextRutPa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/custom_person_icon"
                    android:drawablePadding="12dp"
                    android:hint="@string/rutPa"
                    android:inputType="textPersonName"
                    android:textColor="@color/black"
                    android:textSize="14sp"
                    tools:ignore="TextContrastCheck" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout9"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:fadeScrollbars="false"
                android:importantForAutofill="no"
                app:errorEnabled="true"
                app:layout_constraintBottom_toTopOf="@+id/textInputLayout10"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/textInputLayout8"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout8"
                tools:ignore="TextContrastCheck,TouchTargetSizeCheck">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editTextNombrePa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/custom_person_icon"
                    android:drawablePadding="12dp"
                    android:hint="@string/nombre"
                    android:inputType="textPersonName"
                    android:textColor="@color/black"
                    android:textSize="14sp"
                    tools:ignore="TextContrastCheck" />

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

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout10"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="1dp"
                android:fadeScrollbars="false"
                android:importantForAutofill="no"
                app:errorEnabled="true"
                app:layout_constraintBottom_toTopOf="@+id/textInputLayout11"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/textInputLayout9"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout9"
                tools:ignore="TextContrastCheck,TouchTargetSizeCheck">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editTextApellidoPa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/custom_person_icon"
                    android:drawablePadding="12dp"
                    android:hint="@string/apellidos"
                    android:inputType="textPersonName"
                    android:textColor="@color/black"
                    android:textSize="14sp"
                    tools:ignore="TextContrastCheck" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout11"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:fadeScrollbars="false"
                android:importantForAutofill="no"
                app:errorEnabled="true"
                app:layout_constraintBottom_toTopOf="@+id/textInputLayout12"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout10"
                tools:ignore="TextContrastCheck,TouchTargetSizeCheck">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editTextEmailPa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/custom_email_icon"
                    android:drawablePadding="12dp"
                    android:hint="@string/emailPa"
                    android:inputType="textEmailAddress"
                    android:textColor="@color/black"
                    android:textSize="14sp"
                    tools:ignore="TextContrastCheck" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout12"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:fadeScrollbars="false"
                android:importantForAutofill="no"
                app:errorEnabled="true"
                app:layout_constraintBottom_toTopOf="@+id/textInputLayout13"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout11"
                tools:ignore="TextContrastCheck,TouchTargetSizeCheck">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editTextPhonePa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/custom_phone_icon"
                    android:drawablePadding="12dp"
                    android:hint="@string/n_mero_de_celularPa"
                    android:inputType="phone"
                    android:textColor="@color/black"
                    android:textSize="14sp"
                    tools:ignore="TextContrastCheck" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout13"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:fadeScrollbars="false"
                android:importantForAutofill="no"
                app:errorEnabled="true"
                app:layout_constraintBottom_toTopOf="@+id/textInputLayout14"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout12"
                tools:ignore="TextContrastCheck,TouchTargetSizeCheck">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editTextMotivoConsultaPa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/custom_phone_icon"
                    android:drawablePadding="12dp"
                    android:hint="@string/motivo_de_consulta"
                    android:inputType="text"
                    android:textColor="@color/black"
                    android:textSize="14sp"
                    tools:ignore="TextContrastCheck" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout14"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:fadeScrollbars="false"
                android:importantForAutofill="no"
                app:errorEnabled="true"
                app:layout_constraintBottom_toTopOf="@+id/buttonAgregarPa"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout13"
                tools:ignore="TextContrastCheck,TouchTargetSizeCheck">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editTextDiagnosticoPa"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableStart="@drawable/custom_phone_icon"
                    android:drawablePadding="12dp"
                    android:hint="@string/diagnostico"
                    android:inputType="text"
                    android:textColor="@color/black"
                    android:textSize="14sp"
                    tools:ignore="TextContrastCheck" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.button.MaterialButton
                android:id="@+id/buttonAgregarPa"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="@string/agregarPa"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout14"
                tools:ignore="TouchTargetSizeCheck" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 给父ConstraintLayout加一些padding,或者给maxHeight = "matchparent"
  • 我试过了,但没用

标签: android android-studio android-layout button


【解决方案1】:

幸运的是ConstraintLayout 可以扁平化视图层次结构。这意味着您应该避免将控制权交给CardView,并将CardViewConstraintLayout2 置于同一级别。意思是使用CardView 作为纯粹的视图,它只是为了展示。

<androidx.constraintlayout.widget.ConstraintLayout
        android:elevation="11dp" <- If you want to keep the CardView elevated
        android:id="@+id/ConstraintLayout2"
        app:layout_constraintBottom_toBottomOf="@+id/cardView"
        app:layout_constraintEnd_toEndOf="@+id/cardView"
        app:layout_constraintStart_toStartOf="@+id/cardView"
        app:layout_constraintTop_toTopOf="@+id/cardView"
        android:layout_marginTop="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        android:layout_width="0dp"
        android:layout_height="0dp">
        <!-- All your TextViews and EditTexts HERE-->
        
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.cardview.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="0dp" <- Change to 0dp to fill space
    android:layout_height="0dp" <- Change to 0dp to fill space
    android:layout_marginTop="10dp"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginBottom="10dp"
    app:cardBackgroundColor="@color/white"
    app:cardCornerRadius="15dp"
    app:cardElevation="-1dp" <- CardViews have inherent elevation you need to get rid of it or elevate everything else above it
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>

【讨论】:

  • 感谢您的回答!但是当我应用您的答案link 时发生这种情况我试图更改以匹配 CardView 中的父级但没有得到更好的结果link 据我了解 textview 没有被放置在 cardview 中,会有解决方案吗?或者有更好的方法来获得那种统一的白色背景?
  • 也许尝试颠倒 xml 中的顺序。将 CardView 放在 xml 和 ConstraintLayout 的底部,上面有所有的 textview 和编辑。但保持约束不变。
  • 是的,我试过了,但同样的事情发生了,就像内容不适应5英寸的屏幕,在上级,因为有更多的空间,一切都很好,但我不知道为什么它不适应
  • 你能重新发布你所拥有的 CardViewContraintLayout2 分开的东西,如果没有一点点花哨的话,它应该可以工作。
  • 这里你可以看到我曾经把cardview放下link