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>
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
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?