【问题标题】:Set a max spacing between two views using Android's ConstraintLayout使用 Android 的 ConstraintLayout 设置两个视图之间的最大间距
【发布时间】:2019-08-14 19:36:54
【问题描述】:

我想知道是否有任何方法可以在 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>

【问题讨论】:

  • 请说明您希望达到的目标
  • 你可以使用packed chain 并给margins 它对于所有屏幕尺寸都是恒定的..

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


【解决方案1】:

使用&lt;Space&gt; 设置垂直或水平间距。关键是将宽度或高度设置为0dp,以设置元素之间的最大空间,或固定间距的任何值(20dp):

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

【讨论】:

    【解决方案2】:

    如果你想设置自定义间距(最大 20dp)那么你应该使用这个:

    app:layout_constraintHorizontal_chainStyle="packed"
    

    然后您可以使用边距添加视图之间的间距,使其成为它们之间的最大间距。

    <?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 的最大间距在哪里?
    • 非常感谢您的编辑。但是现在我有第二个问题:如果屏幕太小以至于 20dp 的间距太大了怎么办?使用您的解决方案,间距将固定为 20dp,不允许低于此值。
    • 感谢另一个奇怪的问题。 1)我认为现在使用的小屏幕不多(除非你正在使用 android wear!!)。 2)我不知道你在问什么。不过,您要实现的目标的图像会有所帮助。
    • @AugustoCarmo:如果你不想硬编码 20dp。最好的方法是保存在dimen的文件中,然后根据分辨率、屏幕宽度(sw)等创建不同的dimen。检查这个链接:developer.android.com/training/multiscreen/screensizes