I have several nested layouts that I'm trying to rotate 90 degrees on demand in code. I have a setRotation function that works fine, but unfortunately with rotation it does not change completely correctly. The width on these elements is set to match_parent, and after rotation, it still matches the parent width, not the parent height that it should match.
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>
Spinning java code
view.findViewById(R.id.topPlayers).setRotation(90);
This picture shows the image before the start of rotation.
This image shows the image after rotation.
As you can see, the whole box was rotated after the height and width were already set. In the non-rotating version, the width (match_parent) should be full, and the height (layout_weight = 2) should be 2 / 3rds of the screen. This works great. The problem is that after its rotation, these dimensions remain unchanged, and do not adapt and change to a new rotation.
I chose bright background colors to help troubleshoot that the Pink you see is in the main LinearLayout, and not in any of the layouts that rotate.
I tried to enable rotation in the XML itself, and not inside the code, and got the same results as after the picture, so I clearly understand that I donβt understand how to get the width of the layout as I want. Can I just not use layout_weight efficiently with rotation?
android android-layout rotation android-xml android-layout-weight
Jeff mcaleer
source share