0

Im using a Constraintlayout with bias to fill the screen with multiple views. When I rotate the views they dont get resized to to fill the screen. My layout is more complicated but I created an example to show my problem.

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_dark"
        app:layout_constraintBottom_toTopOf="@+id/two"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:rotation="90"
            android:background="@android:color/holo_green_light"/>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/two"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/one">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_light" />
    </FrameLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

Screen

It doesn't really matter if I rotate the outer or the inner FrameLayout. I don't think I had this problem with LinearLayouts maybe the constraints are getting messed up by the rotation?

EDIT: Hmm looks like the same is happening when using a Linearlayout with weight as parent, so Im probably just doing something wrong here.

TeKo
  • 465
  • 1
  • 5
  • 17
  • What do you mean by rotating views? rotating view with device rotation or anything else? – Md. Yamin Mollah Feb 01 '20 at 15:39
  • android:rotation="90" – TeKo Feb 01 '20 at 16:01
  • Rotation is just an effect and it has no effect on the layout parameters. There is no layout that will handle it for you, you will have to manually calculate and apply appropriate `scaleX` and `scaleY` to confine the view within its bounds. – Pawel Feb 01 '20 at 17:30

1 Answers1

0

The view properties of rotation, translationX and translationY all take effect post-layout. I think that this is true for all view groups. In other words, the views are laid out as if rotation was not specified. Then, after layout, the rotation is applied. This is what you are seeing.

I don't have a reference for this but this problem comes up a lot on Stack Overflow.

Here is an example of this using translationY. Look at the "clarification" section at the top. See how the bottom view does not move even though it is constrained top-to-bottom with the view above? That is because it is positioned to the top view before the top view moves. translationY happens post-layout as does rotation.

This problem can be solved with (probably) a little coding. The exact solution depends on what you are trying to do.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131