constraintlayout icon indicating copy to clipboard operation
constraintlayout copied to clipboard

MotionLayout: Placing a view off-screen can cause it to have wrong dimensions/position

Open Stonos opened this issue 3 years ago • 1 comments

If you place a View off-screen such that its left and/or top positions are negative, then that View will have a position that's off by 2px, and a dimension that's off by 1px. For example:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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="match_parent"
    app:layoutDescription="@xml/off_screen_scene">

    <View
        android:id="@+id/viewOnScreen"
        android:layout_width="100px"
        android:layout_height="100px"
        android:background="#00ff00"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/viewOffScreen"
        android:layout_width="100px"
        android:layout_height="100px"
        android:background="#ff0000"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="parent" />
</androidx.constraintlayout.motion.widget.MotionLayout>

off_screen_scene.xml:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ConstraintSet android:id="@+id/start" />

    <ConstraintSet android:id="@+id/end" />

    <Transition
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@+id/start" />
</MotionScene>

If we take a look at the layout inspector, we can see that viewOnScreen has the correct width/height (100 x 100) and the correct x/y (0 x 0):

image

However, if we take a look at viewOffScreen, we can see that it has wrong dimensions (99 x 99) and position (-98 x -98):

image

Stonos avatar Mar 31 '22 14:03 Stonos

Can confirm this has happened to me as well as I was attempting to put a view outside another in order to achieve.an "outside stroke".

oscarb avatar Jun 09 '22 13:06 oscarb