三个水平按钮,使用ConstraintLayout在它们之间留有少量空白

时间:2019-03-16 09:15:22

标签: android android-layout android-constraintlayout constraint-layout-chains

我想为calc应用程序绘制一个带有大按钮的键盘,每行三个,在这三个按钮之间我需要留一个小的空白。我知道如何使用LinearLayout做到这一点,但是我完全忽略了如何使用约束布局来实现这一点。

我试图在按钮之间分配一定的距离,将中间的按钮居中,然后将左右两个按钮限制在中央按钮上。 结果很糟糕,因为使用不同的屏幕时,按钮之间的距离可能会变得很大,而我想在所有屏幕中达到相同的比例,换句话说,按钮之间的距离(小边距)应始终很小,不受屏幕的影响,几乎用按钮填充了整个屏幕表面,而不用了空白空间。

1 个答案:

答案 0 :(得分:2)

要在constraintLayout中实现此目的,您可以使用障碍/准则/链。

最简单的解决方案是使用链条:

<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/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu">


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button2"
    app:layout_constraintEnd_toStartOf="@+id/button2"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button2" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/button3"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button2"
    app:layout_constraintTop_toTopOf="@+id/button2" />