微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 链中的边距如何在ConstraintLayout 1.1.0(beta)中工作

自从转换到ConstraintLayout版本1.1.0-beta4后,我的几个布局都爆炸了.在进行任何更改之前,我希望更好地了解边距在ConstraintLayout链中的工作原理.在下面,我将ConstraintLayout版本1.0.2中的布局与版本1.1.0-beta4进行比较,但我认为该问题首先出现在1.1.0-beta2中.

我的目标是让一些文本视图在屏幕上伸展,第一和第二文本视图之间以及第二和第三文本视图之间存在间隙.背景应显示在这些边距中.为此,我创建一个水平链并指定从左侧文本视图到中心文本视图的结束边距以及从中心文本视图到右侧文本视图的结束边距.水平链样式是spread_inside.

示例1 – 使用ConstraintLayout 1.0.2版

这就是版本1.0.2中的内容,并且是我所期望的.

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/holo_blue_light">

    <TextView
        android:id="@+id/tvLeft"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_marginEnd="8dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="Text1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tvCenter"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/tvCenter"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_marginEnd="8dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="Text2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tvRight"
        app:layout_constraintStart_toEndOf="@+id/tvLeft"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/tvRight"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="Text3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvCenter"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

</android.support.constraint.ConstraintLayout>

示例2 – 使用ConstraintLayout版本1.1.0-beta4

这种布局在ConstraintLayout的1.1.0-beta4版本中如下所示.请注意,边距已消失.我希望这看起来与示例1相同,但事实并非如此.

示例3 – 使用带有起始边距的ConstraintLayout版本1.1.0-beta4

如果我采用相同的布局并简单地在右侧文本视图(tvRight)中添加8dp的起始边距,我的边距不仅会出现在中间和右侧文本视图之间,还会出现在左侧和中间文本视图之间,尽管我没有更改边距那里.

这不仅仅是之前设定的利润突然被尊重.如果我将最右边文本视图的起始边距设置为“48dp”,则左侧和中心文本视图之间也会出现48dp的边距.

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/holo_blue_light">

<!-- TextViews tvLeft & tvRight not shown but are the same as above.-->

<TextView
    android:id="@+id/tvRight"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_marginStart="48dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:text="Text3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/tvCenter"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="HardcodedText" />

</android.support.constraint.ConstraintLayout>

所以,我的问题是,“为什么我看到这些结果?”如何在ConstraintLayout链中处理边距,尤其是spread_inside链?链条利润的处理方式是否发生变化,或者我遗漏了什么?我正在寻找解释或参考一些解释所有这些的文档.

解决方法

我找不到能够给出这个确切问题的权威答案的文档.但是,在 the API documentation for ConstraintLayout中有一些关于利润率的讨论:

If side margins are set,they will be applied to the corresponding constraints (if they exist)

在链的特定实例中,每个视图之间都有双向约束.也就是说,View A的结束不仅限制在View B的开始,而且View B的开始也受限于View A的结束.

在您发布的布局中,视图A具有结束约束和结束边距,但视图B具有没有开始边距的开始约束.据我所知,这意味着您的布局中存在冲突的规则(视图A希望距视图B 8dp,但视图B希望从视图A获得0dp).也许不同版本的ConstraintLayout库有不同的策略,以便(a)确定这是否算作冲突,(b)如果是这样解决冲突.

通过实验,以下是我在不同的ConstraintLayout库版本中找到链接工作的方式:

版本1.0.2

链中每个视图的边距不依赖于或影响链中的其他视图.这对行为有(至少)两个可见影响.首先,向一个视图添加边距会将另一个视图推离该数量,无论该视图的边距如何.其次,向一个视图添加边距不会影响链条下方的视图边距(例如,在第一个视图上放置8dp结束边距本身也不会导致在第二个和第三个视图之间出现8dp的空间).

版本1.1.0-beta4

链中每个视图的边距都依赖于并影响链中的其他视图.同样,这对行为有两个明显的影响.首先,为一个视图添加边距不会推动另一个视图,除非它还具有相同数量的边距*.其次,在链的第一和第二视图之间添加边距也将影响链**的第二和第三视图之间的间距.

*:似乎1.1.0-beta4只允许一个起始边距推动视图分开,而只是一个结束边距将没有影响.无论如何,我建议匹配边距.

**:我怀疑这是因为连锁店试图均匀地分配“空间”.视图A和B之间的边距产生间隙,并且由于链想要实施一致的间距,因此在视图B和C之间增加了类似的间隙.

例子:

剥离方式,这里的布局与原始布局一样,边距略有变化.我把所有其他属性保持不变.

<android.support.constraint.ConstraintLayout>

    <TextView
        android:layout_marginEnd="8dp"/>

    <TextView
        android:layout_marginStart="8dp"/>

    <TextView/>

</android.support.constraint.ConstraintLayout>

V1.0.2:

V1.1.0-BETA4:

这应该说明库版本之间的两个差异.再一次,我完全无法找到解释所有这些的官方文档,但它似乎只是基于实验.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐