gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:22 25 4
gpt4 key购买 nike

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

This picture显示旋转发生前的图像。

This picture显示旋转发生后的图像。

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

我添加了一些明亮的背景颜色来帮助解决问题,您看到的粉红色是在主 LinearLayout 中,而不是在任何旋转的布局中。

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

最佳答案

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

answer对同一个问题可能会对您有所帮助。

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


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

这是一个project on GitHub这演示了以编程方式进行轮换。暂停三秒后 View 旋转。

希望对您有所帮助。

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>

关于android - 使用setRotation后如何在linearLayout中刷新match_parent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32640274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com