ConstrainLayout ConstraintSet - not working properly with Start / End constraints

It seems that ConstraintSetit's hard to handle the constraints Start/End.

This example is taken from Google examples. Github: android-ConstraintLayoutExamples


When you replace Left and Right constraints with Start& End, ConstraintSet - does not work as expected, only constraints work Left/Right.
For example, replace layout_constraintStart_toStartOf with layout_constraintLeft_toLeftOf and replace layout_constraintEnd_toEndOf with layout_constraintRight_toRightOf

in the following files:
constraintset_example_main.xml
constraintset_example_big.xml

Behavior:

click on image:

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?

+4
3

ConstraintSet, . sample project .

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();
        }
    }
}
+4

, , , , , clone applyTo API, , - , , .

0
source

Source: https://habr.com/ru/post/1694766/


All Articles