如何在约束布局中删除视图之间的多余空间?

问题描述 投票:0回答:1

我已经使用android studio中的约束布局为对话框创建了xml布局。按钮下方有一些额外的空间。没有为视图设置填充,底部页边距也为0dp。按钮下方仍留有空白。我想将按钮对齐到约束布局的底部,没有空白。如何删除该空间?

enter image description here

<?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="wrap_content"
tools:context=".MainActivity">

<TextView
    android:id="@+id/alert_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:text="TextView"
    android:textAlignment="center"
    android:textSize="22sp"
    app:layout_constraintBottom_toTopOf="@+id/exit"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/exit"
    style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Exit"
    android:textSize="18sp"
    app:cornerRadius="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/cancel"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/alert_text" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/cancel"
    style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Cancel"
    android:textSize="18sp"
    app:cornerRadius="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/exit"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/exit"
    app:layout_constraintTop_toTopOf="@+id/exit" />
</androidx.constraintlayout.widget.ConstraintLayout>
android dialog android-constraintlayout
1个回答
0
投票

考虑到按钮上没有“ marginBottom”属性,该间隙是由于按钮的实际背景引起的。有两种方法可以在仍使用按钮的情况下解决此问题:

1)为每个按钮使用不同的背景(即使您使用的不是高架背景也有一定的余量)

2)在android:layout_marginBottom属性上使用负边距。我相信您可以通过反复试验找到确切的价值。

© www.soinside.com 2019 - 2024. All rights reserved.