ConstraintLayout: override baseline constraint in case the visibility of the dependent view was set to GONE
Is there a way to override the base constraint for the TextView in case the view's dependent visibility was set to GONE?
My layout code:
<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>
In this case, my layout looks like this:
When I set the subtitle2
visibility of the TextView to GONE
, my layout looks like this:
So I'm wondering if there is some kind of limitation that can rebuild the baseline in the absence of a dependent view.
source to share
Alternative margins can be specified for widgets GONE
(see Margins when connected to a GONE widget ), but I don't know of such an alternative for baselines.
One way to satisfy your requirement is to specify 0dp
invisible TextView
, which has the same characteristics GONE
TextView
that are located next to the same view. subtitle1
and subtitle2
will snap to the baseline of this new one TextView
. Even though the new TextView
one is not displayed on the screen and takes a zero width, it still maintains a baseline.
Now that it's subtitle2
done GONE
, it will subtitle1
move to the center, but keep its vertical position. (Note 0dp
and invisible
zero text is superfluous, but makes it clear that the widget does not appear.)
Here is the XML with these changes:
<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>
source to share