首页 > 解决方案 > 约束布局和视图旋转不调整视图大小

问题描述

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

    <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>

屏幕

旋转外部或内部 FrameLayout 并不重要。我认为LinearLayouts没有这个问题,也许约束被旋转弄乱了?

编辑:嗯,当使用带有权重的线性布局作为父级时,看起来同样的情况发生了,所以我可能只是在这里做错了。

标签: androidkotlinandroid-constraintlayout

解决方案


rotationtranslationXtranslationY的视图属性都在布局后生效。我认为这适用于所有视图组。换句话说,视图的布局就像没有指定旋转一样。然后,在布局之后,应用旋转。这就是你所看到的。

我没有这方面的参考,但这个问题在 Stack Overflow 上出现了很多。

这是一个使用translationY的例子。查看顶部的“澄清”部分。看看底部视图如何不移动,即使它被上面的视图从上到下约束?那是因为它在顶视图移动之前定位到顶视图。translationY和rotation一样在布局后发生。

这个问题可以通过(可能)一点编码来解决。确切的解决方案取决于您要执行的操作。


推荐阅读