BeL*_*bda 1 android android-layout android-constraintlayout
I have a constraint layout with a simple card view with a text inside, and a text at the bottom. I want the card's width to be at least the width of the bottom text. So something like setting a minimum width.
I drew a picture illustrating what I want but wasn't able to implement it in Constraint Layout.
Barriers will do the magic for you!
With this feature you automatically get the width of the widest object in a certain scope.
From Android Documentaion:
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.
The following xml will check the widest width of the views content_text and bottom_text
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:constraint_referenced_ids="content_text,bottom_text" />
Run Code Online (Sandbox Code Playgroud)
Now you can use this barrier as a constrain in any other view (inside the same ConstraintLayout)
<View
android:id="@+id/card_background"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="@id/content_text"
app:layout_constraintEnd_toEndOf="@id/barrier"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/content_text" />
Run Code Online (Sandbox Code Playgroud)
That's what my result looks like:
My complete XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
tools:context=".MainActivity">
<View
android:id="@+id/card_background"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="@id/content_text"
app:layout_constraintEnd_toEndOf="@id/barrier"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/content_text" />
<TextView
android:id="@+id/content_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="This is a very large content text. It is so long that it needs a second line"
android:textColor="@android:color/black"
android:textSize="22sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/bottom_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a bottom text with a particular length"
android:textColor="@android:color/black"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/card_background" />
<!-- Magic happens here -->
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:constraint_referenced_ids="content_text,bottom_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
Hope this helps.
Here could you read more about barriers
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |