Solution 1 :
This can be accomplished by establishing two 200dp x 200dp Space widgets outside the bounds of the ConstraintLayout. (This works, but I am not necessarily recommending its use. My only objection would be that it is kinda hackish.)
Create two space widgets that are 200dp x 200dp. Constrain the lower right corner of one to the upper left corner of the ConstraintLayout and the upper left corner of the other widget to the bottom right corner of the layout. Now constrain your view to the outer boundaries of these Space widget. You view will not stretch 200dp
outside the boundary of the ConstraintLayout on all sides.
<androidx.constraintlayout.widget.ConstraintLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".MainActivity">
<Space
android_id="@+id/topLeft"
android_layout_width="200dp"
android_layout_height="200dp"
app_layout_constraintBottom_toTopOf="parent"
app_layout_constraintEnd_toStartOf="parent"/>
<Space
android_id="@+id/bottomRight"
android_layout_width="200dp"
android_layout_height="200dp"
app_layout_constraintTop_toBottomOf="parent"
app_layout_constraintStart_toEndOf="parent"/>
<View
android_id="@+id/backgroundView"
android_layout_width="0dp"
android_layout_height="0dp"
android_background="@drawable/main_background"
app_layout_constraintEnd_toEndOf="@id/bottomRight"
app_layout_constraintBottom_toBottomOf="@id/bottomRight"
app_layout_constraintStart_toStartOf="@id/topLeft"
app_layout_constraintTop_toTopOf="@id/topLeft" />
</androidx.constraintlayout.widget.ConstraintLayout>
In the image below, you can see the faint outline of the Space widgets.
Problem :
I have an Activity with ConstraintLayout, and a view in it:
<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"
tools_context=".presentation.MainActivity">
<View
android_id="@+id/backgroundView"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_background="@drawable/main_background" />
I want this view to be bigger than its parent for, for example, 200dp
by each side.
On iOS I can just constraint it by (-Npt, -Npt, Npt, Npt) (left, top, right, bottom) to achieve this (on this screen target view is background view with pattern image):
But how to correctly do it on Android with ConstraintLayout
Comments
Comment posted by CommonsWare
That’s not usually done in Android. The closest thing we have ever done for that involved negative margins, and AFAIK those are not supported with
Comment posted by artem
@CommonsWare yep, negative margins do no work.