0

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

我试图在按钮之间分配一些距离,使中央按钮居中,然后将左右按钮限制在中央按钮上。结果很糟糕,因为使用不同的屏幕按钮之间的距离可能会变得很大,而我想在所有屏幕中实现相同的比例,换句话说,按钮之间的距离(小边距)应该总是很小,独立于屏幕,几乎所有的屏幕表面都被按钮而不是空白区域填满。

4

1 回答 1

2

要在约束布局中实现这一点,您可以使用障碍/指南/链。

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

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

于 2019-03-16T10:04:53.370 回答