Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set constraint to two views?

So I have a constraintLayout like this

<ConstraintLayout>
    <TextView
    android:id="@+id/text_view"
    app:layout_constraintEnd_toStartOf="@+id/view1" />

    <View
    android:id="@+id/view1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

    <View
    android:id="@+id/view2"
    aapp:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>

Currently the textview's end is at the start of view1, however, when view1's visibility is gone, I want the end of textview to the start of view2, otherwise the long string may cover view2. Is there any way to set the constraint to two views or is there any better solution for this?

like image 205
litaoshen Avatar asked Apr 26 '18 16:04

litaoshen


People also ask

How do I change ConstraintLayout to LinearLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

Is ConstraintLayout faster than LinearLayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.

What is groups in constraint layout?

Groups is used when you want to handle visibility or motion with all its view. The Group in ConstraintLayout is just a loose association of views. Guideline: A guideline is a visual guide that will not be seen at runtime that is used to align other views.

WHAT IS barrier in constraint layout?

A Barrier references multiple widgets as input, and creates a virtual guideline based on the most extreme widget on the specified side. For example, a left barrier will align to the left of all the referenced views.


1 Answers

You can use a Barrier to constraint to multiple views.

<ConstraintLayout>

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        app:barrierDirection="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="view1,view2"/>    

    <TextView
        android:id="@+id/text_view"
        app:layout_constraintEnd_toStartOf="@id/barrier" />

    <View
        android:id="@+id/view1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <View
        android:id="@+id/view2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>

The textview will be constraint to the view that is more to the left

like image 132
ZeRj Avatar answered Dec 23 '22 18:12

ZeRj