【问题标题】:Button doesn't fully stick to bottom of screen in constraint layout按钮在约束布局中没有完全粘在屏幕底部
【发布时间】:2020-05-24 18:58:06
【问题描述】:

我将一个按钮约束在屏幕底部,可惜屏幕底部边框和按钮底部边框之间有一个小间隙:

我的 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>

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

【问题讨论】:

  • android studio 有一个名为“layout inspect”的功能。您可以在工具选项下找到它。您可以使用此功能检查布局以检查导致间隙的原因!希望有帮助!

标签: android android-constraintlayout android-button


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    将此代码添加到 &lt;androidx.constraintlayout.widget.ConstraintLayout ...&gt;&lt;button ...&gt;

    android:layout_margin="0dp"
    android:padding="0dp"
    


    将此代码添加到 &lt;com.google.android.material.tabs.TabLayout ...&gt;&lt;LinearLayout ...&gt;&lt;button ...&gt;

    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    


    将此代码添加到 &lt;button ...&gt;

    android:background="#c0c0c0"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      相关资源
      最近更新 更多