1

I am going to create a layout with a status bar on the right

I tried to use xml, but this is not work, the bar cannot align to right, and the textview is not in the relativelayout after rotation.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="test.com.myapplication.MainActivity">

    <RelativeLayout
        android:id="@+id/top_bar_land"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:rotation="90"
        android:background="@android:color/holo_red_light">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </RelativeLayout>
</RelativeLayout>

enter image description here

So I rotation it programmatically

topBarLand = (RelativeLayout) findViewById(R.id.top_bar_land);
ViewTreeObserver vto = topBarLand.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int w = topBarLand.getWidth();
        int h = topBarLand.getHeight();
        topBarLand.setRotation(90f);
        topBarLand.setTranslationX((w - h) / 2);
        topBarLand.setTranslationY(Math.abs(h - w) / 2);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            topBarLand.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            topBarLand.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    }
});

But the width of bar cannot match the height of screen

enter image description here

Even I set a large number to its with, the width still no changes.

RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(1920, h);
topBarLand.setLayoutParams(param);

How can I set the bar width same to screen height?

cpt. Sparrow
  • 415
  • 8
  • 22
CL So
  • 3,647
  • 10
  • 51
  • 95
  • buddy that red color view take width only because when it horizontal it take fill_parent , if you rotate 90'c it take same height(i mean it take width size from height) – Vadivel Nov 22 '16 at 11:35
  • By default Android rotates using the view's mid-point as the pivot. You can use `transformPivotX` and `transformPivotY` to avoid manual rotation. However, maybe looking into some vertical text view implementations is a way to go instead of rotation. – Marcin Jedynak Nov 22 '16 at 11:44

1 Answers1

1

you can use this library, add:

dependencies {
    compile 'rongi.rotate-layout:rotate-layout:2.0.0'
}

and format your layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent">    

    <com.github.rongi.rotate_layout.layout.RotateLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        app:angle="-90"> <!-- Specify rotate angle here -->

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World, I'm rotated now!" />
        </RelativeLayout>

    </com.github.rongi.rotate_layout.layout.RotateLayout>

</RelativeLayout>

Result:

Result

Aleksandar G
  • 1,163
  • 2
  • 20
  • 25