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

9

我有几个嵌套的布局,想在代码中按需旋转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>

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

这张图片显示了旋转之前的图像。

这张图片显示了旋转之后的图像。

可以看到,在高度和宽度已经设置之后,整个框被旋转了。在未旋转的版本中,宽度(match_parent)应该是全屏幕,而高度(layout_weight=2)应该是屏幕的2/3。这很好地发挥了作用。问题在于旋转后,这些尺寸保持不变,而不是适应并改变为新的旋转。

我添加了一些明亮的背景色来帮助排除故障,你看到的粉色是在主LinearLayout中,而不是任何旋转的布局中。

我尝试将旋转包含在xml本身中,而不是在代码中,结果得到了与之后的图片完全相同的结果,所以显然我不理解如何获得所需的布局宽度。我不能有效地使用旋转的layout_weight吗?


说实话,我从未解决过这个问题。最终,我创建了单独的XML文件,分别用于旋转和不旋转,并使用自定义的VerticalTextView(如此处所建议的)来使我的文本旋转。虽然多个XML文件比简单的旋转要麻烦一些,但它工作得很好。 - Jeff McAleer
我还没有一个很好的答案,但我认为可以通过获取视图父级的尺寸并设置视图的尺寸来实现。即 int width = ((View) myView.getParent()).getWidth();, myView.setLayoutParams(new FrameLayout.LayoutParams(width, height));。不过我有一些位置问题,但我认为我可以通过使用 setPivotX()setX() 的组合来克服这些问题。 - VinceFior
当您旋转视图时,请交换布局参数的高度和宽度值,并重置视图的布局参数。 - Jayakrishnan
1个回答

1
我认为您遇到的问题与Stack Overflow question中所述的问题相同。似乎旋转没有考虑到布局目的。换句话说,布局发生,然后在旋转之前将视图放置在其上。
那篇答案可能会对您有所帮助。
您也可以手动调整尺寸。无论哪种方式都有点麻烦,但我认为这就是它的方式。
你可以加载一个替代布局,以实现您所需的行为,而不是进行旋转操作。(请参见下面的布局和图像。)您也可以通过以编程方式更改布局参数来实现相同的结果。
这里有一个 GitHub上的项目演示了如何通过编程方式进行旋转。视图在三秒的暂停后旋转。
希望这对你有所帮助。

enter image description here

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接