如何将旋转的布局对齐并匹配父高度?

时间:2016-11-22 11:08:42

标签: android android-layout relativelayout android-xml android-layoutparams

我将在右侧创建一个带状态栏的布局

我尝试使用xml,但这不起作用,条形图无法正确对齐,并且文本视图在旋转后不在relativelayout中。

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

所以我以编程方式轮换它

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);
        }
    }
});

但是条形的宽度与屏幕的高度不匹配

enter image description here

即使我设置了一个大数字,宽度仍然没有变化。

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

如何将条宽设置为与屏幕高度相同?

1 个答案:

答案 0 :(得分:1)

您可以使用this library,添加:

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

并格式化您的布局:

<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