Align ConstraintLayout item to be at the end of two items

前端 未结 2 1334
萌比男神i
萌比男神i 2021-02-13 06:02

I have a ConstraintLayout with two views A and B that are stacked vertically. I have a third view C which needs to be to the end of both A and B horizontally. At any given point

相关标签:
2条回答
  • 2021-02-13 06:24

    This can be easily achieved using the new Barriers feature.

    <?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"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/view_c"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="View C"
            app:layout_constraintLeft_toRightOf="@+id/barrier1"
            app:layout_constraintTop_toTopOf="parent" />
    
        <android.support.constraint.Barrier
            android:id="@+id/barrier1"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            app:barrierDirection="right"
            app:constraint_referenced_ids="view_a, view_b"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/view_a"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="View A"
            app:layout_constraintBottom_toTopOf="@id/view_b"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_goneMarginBottom="16dp" />
    
        <TextView
            android:id="@+id/view_b"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:text="View B"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view_a" />
    
    </android.support.constraint.ConstraintLayout>
    

    How to use Barriers ?

    Barriers are like building a HUUUUGE WALL that "protects" the views mentioned in constraint_referenced_ids. So, you have to mention which direction it's "supposed to keep out" which in our case is the right (view_c). Just use the barrierDirection attribute.
    Finally, don't forget to make sure that view_c is in the keep out zone (layout_constraintLeft_toRightOf="@+id/barrier1").

    As this feature is available only in the 1.1.0 beta1 release of ConstraintLayout, don't forget to add this line to your build.gradle file.

    compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
    


    Hope this helps!

    0 讨论(0)
  • 2021-02-13 06:37

    I tried this out in my IDE and i came up with some code to do it dynamically at runtime

    TextView viewa = (TextView) findViewById(R.id.view_a);
    TextView viewb = (TextView) findViewById(R.id.view_b);
    
    ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.constraintLayout);
    ConstraintSet cs = new ConstraintSet();
    cs.clone(cl);
    cs.connect(viewb.getWidth() > viewa.getWidth() ? R.id.view_b : R.id.view_a, ConstraintSet.RIGHT, R.id.view_c, ConstraintSet.LEFT);
    cs.applyTo(cl);
    

    This ends up messing it in a slight way as it pushes the view which is wider out of the screen on the left. Maybe you could figure that out as i am unable to at this point.

    0 讨论(0)
提交回复
热议问题