android - 使用 Android 的 ConstraintLayout 设置两个 View 之间的最大间距

标签 android android-constraintlayout constraint-layout-chains

我想知道在 Android 中是否有任何方法可以使用 ConstraintLayout 在使用 Chains 时设置两个 View 之间的最大间距。我知道使用 margin 属性就像两个 View 之间的最小间距一样,但我不知道如何设置最大间距。

例如,我如何通过以下布局 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>

最佳答案

使用 <Space> 设置垂直或水平间距.关键是设置宽度或高度为0dp元素之间的最大空间,或固定间距的任何值(20dp):

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

关于android - 使用 Android 的 ConstraintLayout 设置两个 View 之间的最大间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57493308/

相关文章:

android - fitsSystemWindows on Fragment、Activity 和 DrawerLayout 组合

java - fragment 交易删除

android - fragment 上的 constraintLayout 关键帧动画

android - 当父维度设置为 wrap_content 时,ConstraintLayout Barrier 无法正常工作

android - ConstraintLayout 链和文本省略号 + 右侧的图像

android - 哪些移动网络类型允许通话期间的 http 流量?

android.support.v7.app.ActionBarActivity 已弃用

android - 带数据绑定(bind)的 ConstraintLayout

android - ConstraintLayout GONE View 占用空间

android - 使用约束布局垂直居中多个 View