如何在使用setRotation后刷新linearLayout中的match_parent?

Jef*_*eer 9 android rotation android-layout android-xml android-layout-weight

我有几个嵌套的布局,我试图在代码中按需旋转90度.我已经把setRotation的一部分工作得很好,但不幸的是,事情并没有通过轮换来调整.这些元素的宽度设置为match_parent,旋转后它仍然匹配父宽度,而不是它应匹配的父高度.

XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="link.basiclifecounter.LifeCounter"
    android:background="#CC00CC">

    <LinearLayout
        android:id="@+id/topPlayers"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical"
        android:background="#CC0000">

        <RelativeLayout
            android:id="@+id/p3"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        **A bunch of stuff in here**

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/p2"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        **A bunch of stuff in here**

        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottomPlayer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#00CC00">

        <RelativeLayout
            android:id="@+id/p1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        **A bunch of stuff in here**

        </RelativeLayout>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

旋转Java代码

view.findViewById(R.id.topPlayers).setRotation(90); //Rotate the entire top box
view.findViewById(R.id.p3).setRotation(180); //Flip one side so both players face outwards
Run Code Online (Sandbox Code Playgroud)

此图显示旋转发生前的图像.

此图显示旋转发生后的图像.

如您所见,整个盒子在已设置高度和宽度后已旋转.在未旋转的版本中,width(match_parent)应为全屏,高度(layout_weight = 2)应为屏幕的2/3.这很好用.问题是它旋转后,这些尺寸保持不变,而不是适应和改变到新的旋转.

我投入了一些明亮的背景颜色来帮助排除故障,你看到的粉红色在主要的LinearLayout中,而不是在任何旋转的布局中.

我确实尝试在xml本身中包含旋转,而不是在代码中,并且得到与after图片完全相同的结果,所以很明显我不了解如何以我想要的方式获得布局宽度.我可以不通过旋转有效地使用layout_weight吗?

Che*_*amp 1

我认为您遇到了与Stack Overflow 问题中概述的相同问题。出于布局目的,似乎根本没有考虑旋转。换句话说,布局发生后,旋转发生在旋转之前布局的视图上。

对同一问题的回答可能会对您有所帮助。

您还可以手动调整测量值。无论哪种方式都有点混乱,但我认为就是这样。


您可以加载一个替代布局,该布局将按照您的意愿运行,而不是进行旋转。(请参见下面的布局和图像。)您还可以通过以编程方式更改布局参数来获得相同的结果。

这是GitHub 上的一个项目,演示了如何以编程方式进行轮换。视图在三秒暂停后旋转。

我希望您觉得这很有用。

在此输入图像描述

备用.xml

    <LinearLayout
        android:id="@+id/topPlayers"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#CC0000"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/p3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:rotation="90">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="p3"
                android:textSize="48sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/p2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000CC"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:rotation="-90"
                android:text="p2"
                android:textSize="48sp" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottomPlayer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00CC00"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/p1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="p1"
                android:textSize="48sp" />
        </RelativeLayout>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)