使用Android的ConstraintLayout设置两个视图之间的最大间距

3
我想知道在Android中是否有一种方法可以使用ConstraintLayout,在使用Chains时设置两个视图之间的最大间距。我知道使用margin属性可以像两个视图之间的最小间距一样工作,但我无法找到如何设置最大间距。
例如,我如何通过以下布局xml实现这一点(最大间距= 20dp)?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <View
        android:id="@+id/left_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#FF0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/right_view"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread" />

    <View
        android:id="@+id/right_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#00FF00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/left_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

请解释您想要实现什么。 - Lakhwinder Singh
你可以使用“packed chain”并给出“margins”,这将对所有屏幕尺寸保持不变。 - Santanu Sur
2个回答

3

使用<Space>设置垂直或水平间距。关键在于将宽度或高度设为0dp以最大化元素之间的空间,或者任意值用于固定间距(20dp):

<Space
    android:layout_width="0dp"
    android:layout_height="1dp"/>

2
如果您想设置自定义间距(最大20dp),则应使用以下代码:

最初的回答
app:layout_constraintHorizontal_chainStyle="packed"

然后,您可以使用 margin 在视图之间添加间距,以便它们之间的最大间距。将原始答案翻译成“最初的回答”。
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/left_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginEnd="10dp"
        android:background="#FF0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/right_view"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/right_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="10dp"
        android:background="#00FF00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/left_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

谢谢您的回复,@BirjuVachhani :). 在您的示例中,20dp集合的最大间距在哪里? - Augusto Carmo
非常感谢您的编辑。但是现在我有第二个问题:如果屏幕太小,20dp的间距太大怎么办?使用您的解决方案,间距将被固定为20dp,不允许它低于这个值。 - Augusto Carmo
谢谢提供这样奇怪的问题。1)我认为现在很少使用这么小的屏幕(除非您正在开发Android Wear!)。2)我不认为您所要求的是可能的。但如果您能提供所需实现的图像,则会更有帮助。 - Birju Vachhani
@AugustoCarmo:如果您不想硬编码20dp,最好的方法是将其保存在dimen文件中,然后根据分辨率、屏幕宽度(sw)等创建不同的dimen。请查看此链接:https://developer.android.com/training/multiscreen/screensizes - Akash Patra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接