约束布局和视图旋转不调整视图大小

时间:2020-02-01 15:04:47

标签: android kotlin android-constraintlayout

我使用带有约束的约束布局将多个视图填充到屏幕中。当我旋转视图时,它们不会调整大小以填充屏幕。我的布局更加复杂,但是我创建了一个示例来展示我的问题。

    <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

旋转外部或内部FrameLayout并不重要。我不认为我在LinearLayouts上遇到过这个问题,也许约束因旋转而变得混乱了?

编辑:当使用重量为父级的Linearlayout时,嗯看起来也是如此,所以我可能在这里做错了。

1 个答案:

答案 0 :(得分:0)

旋转 translationX translationY 的视图属性均在布局后生效。我认为所有视图组都是如此。换句话说,视图的布局就像未指定 rotation 一样。然后,在布局之后,应用旋转。这就是您所看到的。

我没有这个的参考,但是这个问题在Stack Overflow上经常出现。

Here是使用 translationY 的示例。查看顶部的“说明”部分。看到底视图从上到下受约束时,底视图如何不移动?那是因为它位于顶视图移动之前 的顶视图。 translationY rotation 一样在布局后发生。

(可能)只需少量编码即可解决此问题。确切的解决方案取决于您要执行的操作。