ConstraintLayout: 当依赖视图的可见性设置为GONE时,重新对齐基线约束。

6

如果依赖视图的可见性被设置为GONE,有没有办法重新对TextView进行基线约束的重新对齐?

我的布局代码:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST TITLE"
        android:textColor="@color/black"
        android:textSize="24sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <TextView
        android:id="@+id/subtitle1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUBTITLE 1"
        android:textSize="11sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/subtitle2"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/subtitleSpace"
        app:layout_constraintRight_toRightOf="parent"/>

    <android.support.v4.widget.Space
        android:id="@+id/subtitleSpace"
        android:layout_width="12dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/subtitle1"
        app:layout_constraintRight_toLeftOf="@+id/subtitle2"/>

    <TextView
        android:id="@+id/subtitle2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="SUBTITLE 2"
        android:textSize="14sp"
        app:layout_constraintLeft_toRightOf="@+id/subtitleSpace"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title"/>

</android.support.constraint.ConstraintLayout>

在这种情况下,我的布局如下: enter image description here 当我将subtitle2 TextView的可见性设置为GONE时,我的布局将如下所示: enter image description here 因此,我想知道是否有一些约束条件可以在依赖视图缺失的情况下重新对齐基线。
2个回答

1

可以为 GONE 小部件指定替代边距(请参见 连接到 GONE 小部件时的边距),但我不知道是否有基线的替代方法。

满足您要求的一种方法是指定一个与放置在同一视图旁边的 GONE TextView 具有相同特性的 0dp 不可见 TextViewsubtitle1subtitle2 将绑定到此新 TextView 的基线。即使新的 TextView 在屏幕上不显示且宽度为零,它仍然保持基线。

现在,当 subtitle2 变为 GONE 时,subtitle1 将移动到中心位置,但保持其垂直位置。(使用空文本指定 0dpinvisible 是多余的,但可以清楚地表明该小部件不应出现。)

这是具有这些更改的 XML:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST TITLE"
        android:textColor="#FF000000"
        android:textSize="24sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/subtitle1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUBTITLE 1"
        android:textSize="11sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/viewForBaseline"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/subtitleSpace"
        app:layout_constraintRight_toRightOf="parent" />

    <android.support.v4.widget.Space
        android:id="@+id/subtitleSpace"
        android:layout_width="12dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/subtitle1"
        app:layout_constraintRight_toLeftOf="@+id/subtitle2" />

    <TextView
        android:id="@+id/subtitle2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="SUBTITLE 2"
        android:textSize="14sp"
        android:visibility="gone"
        app:layout_constraintBaseline_toBaselineOf="@+id/viewForBaseline"
        app:layout_constraintLeft_toRightOf="@+id/subtitleSpace"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title" />

    <TextView
        android:id="@+id/viewForBaseline"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:textSize="14sp"
        android:visibility="invisible"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title" />

</android.support.constraint.ConstraintLayout> 

谢谢您的建议!我已经采用了您提出的相同解决方案,但我仍然想知道是否存在一些限制GONE文本视图基线。我不想在我的布局中留下空视图以避免复杂性并保持更清洁。 - novachevskyi
2
我认为那个问题的答案是没有,但有这样一件事情是有意义的。 - Cheticamp

0

你可以在 XML 代码之外以编程方式实现它。这不是理想的解决方案,但最终可以得到你想要的确切约束集。基本思路:

private val defaultConstrainSet = ConstraintSet()
private val newConstrainSet = ConstraintSet()

init {
    defaultConstrainSet.clone(constraintLayout)
    newConstrainSet.apply {
        clone(constraintLayout)
        // here put your new constrains, eg. :
        clear(R.id.subtitle1, ConstraintSet.BASELINE)
        connect(R.id.agenda_index, ConstraintSet.TOP, R.id.title, ConstraintSet.BOTTOM)
    }
}

然后

    if (/* subtitle2 is visible */) {
        defaultConstrainSet.applyTo(constraintLayout)
    } else {
        newConstrainSet.applyTo(constraintLayout)
    }

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