首页 > 解决方案 > 按钮在约束布局中没有完全粘在屏幕底部

问题描述

我将一个按钮约束到屏幕底部,但不幸的是屏幕底部边框和按钮底部边框之间有一个小间隙:

在此处输入图像描述

我的 XML:

<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">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/navigationTabs"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_constraintTop_toTopOf="parent"
        app:tabMinWidth="100dp"
        app:tabRippleColor="@null"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="BUTTON"
        android:layout_margin="0dp"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <LinearLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/navigationTabs" />


</androidx.constraintlayout.widget.ConstraintLayout>

造成这种差距的原因是什么?我怎样才能摆脱它?

标签: androidandroid-constraintlayoutandroid-button

解决方案


它是默认背景按钮周围的阴影。指定你自己的背景,差距就会消失。例如:

<Button
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#A4ABA4"
    android:text="BUTTON"
    android:layout_margin="0dp"
    app:layout_constraintBottom_toBottomOf="parent"/>

推荐阅读