Reputation: 99
I am using constraint layout as parent, and added multiple child using constraint. There are three views horizontally and I have to apply rounded corner background for two view only, which was achieved by linear layout previously. But with the help of constraint layout, I am not able to achieve this.
How can I achieve this?
Upvotes: 7
Views: 8494
Reputation: 45090
You can do it with simple Image View following this steps:
Put a Image view before text views
set Image view's start, top constraint to match first text view
Set Image view's end & bottom constraint to match last text views
This will gone stretch that imageview
to a Rectangle defined by top-left of first view and bottom-right of last view.
Note that ImageView must be before text views:
<!--Background Image View-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@drawable/lake"
app:layout_goneMarginRight="@dimen/activity_vertical_margin"
<!--Top_LEFT by first view-->
app:layout_constraintStart_toStartOf="heding"
app:layout_constraintTop_toTopOf="@+id/heding"
<!--BOTTOM RIGHT by last view-->
app:layout_constraintEnd_toEndOf="@+id/info"
app:layout_constraintBottom_toBottomOf="@+id/info"
/>
<!--first view-->
<TextView
android:id="@+id/heding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Lake Tahoe "
android:textSize="17sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toEndOf="@+id/thumb"
app:layout_constraintTop_toTopOf="@+id/thumb"
tools:layout_constraintBottom_creator="1"
android:textColor="#FFF"
tools:layout_constraintTop_creator="1" />
<!--last view-->
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="LakeTahoe is a large freshwater lake in Sierra Naveda"
android:textSize="15sp"
android:textColor="#FFF"
app:layout_constraintBottom_toBottomOf="@+id/thumb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toEndOf="@+id/thumb"
app:layout_constraintTop_toBottomOf="@+id/heding"
app:layout_constraintVertical_bias="1.0"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
Result:
Upvotes: 0
Reputation: 1037
ConstraintLayout recently introduced the concept of constrainthelper which can be used to perform an action on a group of views. 'Group', which is used to toggle visibility of multiple view is subclass of this.
A constrainthelper which changes background of multiple views together is not yet a part of the stable release, but soon will be.
Until then, you can achieve a backround as in the example below, where 3 textview share a common background, using an View class:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAA">
<View
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FFF"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="@+id/textView1"
app:layout_constraintStart_toStartOf="@+id/textView1"
app:layout_constraintTop_toTopOf="@+id/textView1" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="@+id/textView1"
app:layout_constraintStart_toStartOf="@+id/textView1"
app:layout_constraintTop_toBottomOf="@+id/textView1"
tools:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="@+id/textView1"
app:layout_constraintStart_toStartOf="@+id/textView1"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:text="TextView" />
</android.support.constraint.ConstraintLayout>
EDIT: A nice blog on what to expect from ConstraintLayout 2.0
Upvotes: 8