Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View is pushed out of its constraints in ConstraintLayout

I have a ConstraintLayout with an ImageView and 3 chained TextViews with a spread_inside chain style:

<android.support.design.card.MaterialCardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/space_normal"
        android:paddingEnd="@dimen/space_normal"
        android:paddingStart="@dimen/space_normal"
        android:paddingTop="@dimen/space_big">

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="@dimen/feed_list_image_size"
            android:layout_height="@dimen/feed_list_image_size"
            android:layout_marginBottom="@dimen/space_normal"
            android:contentDescription="@null"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            tools:src="@color/debug_3" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/space_normal"
            android:ellipsize="end"
            android:maxLines="3"
            android:textSize="@dimen/text_size_big"
            app:layout_constraintBottom_toTopOf="@+id/tvContent"
            app:layout_constraintEnd_toStartOf="@+id/ivImage"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="spread_inside"
            app:textAllCaps="true"
            tools:text="@tools:sample/lorem" />

        <TextView
            android:id="@+id/tvContent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/space_big"
            android:layout_marginTop="@dimen/space_normal"
            android:ellipsize="end"
            android:maxLines="4"
            android:textColor="@color/gray_600"
            android:textSize="@dimen/text_size_normal"
            app:layout_constraintBottom_toTopOf="@+id/tvDate"
            app:layout_constraintEnd_toEndOf="@+id/tvTitle"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvTitle"
            tools:text="@tools:sample/lorem/random" />

        <TextView
            android:id="@+id/tvDate"
            style="@style/AppTheme.ItemFeedList.Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/space_normal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvContent"
            tools:text="@tools:sample/date/hhmm" />

    </android.support.constraint.ConstraintLayout>

</android.support.design.card.MaterialCardView>

This renders a nice and flexible layout in the editor:

layout

layout_short_text

But sometimes the top view is "pushed" out of its contraints so the text rendering is wrong (clipped). It's wierd because spread_inside chain style should inflate and narrow the middle view. From the Layout Inspector:

layout_wierd

What's the wrong with it?

like image 552
Nikolay Kulachenko Avatar asked Jun 06 '18 16:06

Nikolay Kulachenko


People also ask

How do you constrain a view?

To create a baseline constraint, right-click the text view you want to constrain and then click Show Baseline. Then click on the text baseline and drag the line to another baseline.

Why do we prefer constraint ConstraintLayout in Android?

It helps to improve the UI performance over other layouts. With the help of ConstraintLayout, we can control the group of widgets through a single line of code. With the help of ConstraintLayout, we can easily add animations to the UI components which we used in our app.

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.

How is barrier used in ConstraintLayout?

To define a Barrier , you can select one or more View components from the “Design” view, open the “Guidelines” menu and select the Barrier . If you want to add it directly in the XML, you can use the following code snippet: The resulting layout looks like the screenshot of the “Design” layout editor view from below.


1 Answers

A change made in version 1.1 of ConstraintLayout may help you. Try setting app:layout_constrainedHeight=”true” on your troublesome TextView. From the developer guide for ConstraintLayout:

WRAP_CONTENT : enforcing constraints (Added in 1.1)

If a dimension is set to WRAP_CONTENT, in versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attribute:

app:layout_constrainedWidth=”true|false”
app:layout_constrainedHeight=”true|false”

like image 62
Cheticamp Avatar answered Nov 15 '22 07:11

Cheticamp