ConstrainLayout约束集合 - 在使用Start/End约束时无法正常工作

5
看起来,ConstraintSet很难应对Start/End约束。
此示例摘自Google样例。 Github:android-ConstraintLayoutExamples

当您将左侧和右侧的约束替换为StartEnd时,ConstraintSet不能正确工作,它只能与Left/Right约束一起使用。 例如,使用以下方式替换 layout_constraintStart_toStartOf与layout_constraintLeft_toLeftOf ,并用 layout_constraintEnd_toEndOf替换layout_constraintRight_toRightOf
在以下文件中进行替换:
constraintset_example_main.xml
constraintset_example_big.xml

行为:


单击图像:
private ConstraintSet mConstraintSetNormal = new ConstraintSet();

private ConstraintSet mConstraintSetBig = new ConstraintSet();

public void toggleMode(View v) {
    TransitionManager.beginDelayedTransition(mRootLayout);
    mShowBigImage = !mShowBigImage;
    applyConfig();
}

private void applyConfig() {
    if (mShowBigImage) {
       mConstraintSetBig.applyTo(mRootLayout);
    } else {
        mConstraintSetNormal.applyTo(mRootLayout);
    }
}

默认情况下,Android Studio使用start/end约束,因此我想知道根本原因和可能的修复方法。
或者这是ConstrainSet本身的错误吗?


什么是用Start/End替换Left/Right的必要性? - Om Infowave Developers
默认情况下,Android Studio 使用起始/结束约束而不是左/右约束。因此,当我开始创建新的布局时,我发现了这个问题和观察结果。 - AskQ
3个回答

7
这似乎是与ConstraintSet有关的问题,但我们来看看。以下分析基于您提供的链接示例项目
在示例项目中,我已将ConstraintLayout更新为最新版本:
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta5'

我这样做是为了在追踪已解决问题时有所帮助。我还更新了布局constraintset_example_big并将所有的左/右约束替换为如下的开始/结束约束:

constraintset_example_big.xml

<android.support.constraint.ConstraintLayout 
    android:id="@+id/activity_constraintset_example"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:onClick="toggleMode"
        android:scaleType="centerCrop"
        android:src="@drawable/lake"
        app:layout_constraintDimensionRatio="h,16:9"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:contentDescription="@string/lake_tahoe_image" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lake_tahoe_title"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="@string/lake_discription"
        app:layout_constraintStart_toStartOf="@+id/textView9"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textView9"
        app:layout_constraintEnd_toEndOf="@+id/imageView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

在进行这些更改后,我们看到了以下结果。

enter image description here

这显然不对。过渡后应该看起来像 this
经过一些调试,我将问题追踪到 ConstraintSetExampleActivity.java 中的这行代码:
mConstraintSetBig.load(this, R.layout.constraintset_example_big);

ConstraintSet#load() 看起来很简单,但如果我们用显式的布局填充代替上面的代码,并在填充后对克隆的膜拜布局进行 ConstraintSet 的克隆,如下所示:

// mConstraintSetBig.load(this, R.layout.constraintset_example_big);
ConstraintLayout cl = (ConstraintLayout) getLayoutInflater().inflate(R.layout.constraintset_example_big,null);
mConstraintSetBig.clone(cl);

我们在应用程序中看到了这种行为,这要好得多。

enter image description here

我的理解是,ConstraintSet#load() 在处理起始/结束约束时存在问题。解决方法是先填充 ConstraintLayout,然后进行克隆。
ConstraintSetExampleActivity#onCreate() 中实现。
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.constraintset_example_main);

    mRootLayout = (ConstraintLayout) findViewById(R.id.activity_constraintset_example);
    // Note that this can also be achieved by calling
    // `mConstraintSetNormal.load(this, R.layout.constraintset_example_main);`
    // Since we already have an inflated ConstraintLayout in `mRootLayout`, clone() is
    // faster and considered the best practice.
    mConstraintSetNormal.clone(mRootLayout);
    // Load the constraints from the layout where ImageView is enlarged.

    // Toggle the comment status on the following three lines to fix/break.
    // mConstraintSetBig.load(this, R.layout.constraintset_example_big);
    ConstraintLayout cl = (ConstraintLayout) getLayoutInflater().inflate(R.layout.constraintset_example_big,null);
    mConstraintSetBig.clone(cl);

    if (savedInstanceState != null) {
        boolean previous = savedInstanceState.getBoolean(SHOW_BIG_IMAGE);
        if (previous != mShowBigImage) {
            mShowBigImage = previous;
            applyConfig();
        }
    }
}

如果我在onViewCreated上应用它,它根本不适用于片段,只显示最终结果。 - LeTadas

3

2
最初的回答:如果你遇到了像约束集克隆不正常工作这样的问题,当我在API调用后调用克隆和应用方法时,我的布局没有更新到新的约束条件,结果是由于我在更改之前显示了一个加载对话框导致错误。

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