安卓:旋转整个布局 [英] Android: Rotate whole layout

查看:228
本文介绍了安卓:旋转整个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够旋转整个布局上飞(上一个点击按钮)。

I need to be able to rotate whole layouts on the fly (on the click of a button).

我能够用旋转的布局,例如。 layout.setRotation(270.0f)的问题是,在旋转后,布局高度宽度不匹配其父

I am able to rotate the layouts using, eg. layout.setRotation(270.0f). The problem is, after the rotation, the layout height and width are not matching its parent's.

我试图反转高度宽度像这样,

RelativeLayout layout = (RelativeLayout)findViewById(R.id.rootLayout);
LayoutParams layoutParams = layout.getLayoutParams();
int height = layout.getHeight();
int width = layout.getWidth();
layoutParams.height = width;
layoutParams.width = height;

这确实什么都没有。

Which does nothing at all.

我用 SDK 14

下面的第一图像是该应用,因为它启动。第二个,一个旋转后。我想,以填补黑色的空间。任何帮助将是AP preciated。

The first image below is the app as it starts. The second one, after a rotation. I wish to fill the black "space". Any help would be appreciated.

下面显示的图像中的布局仅一个按钮。然而,在现实中,布局是一​​个复杂得多。我试图实现的是伪造一个横向视图。

The images below show only a button in the layout. However, in reality, the layout are a lot more complex. What I am trying to achieve is "faking" a landscape view.

编辑:更改图像和补充说明

推荐答案

不知道为什么,这是非常有用的,但它是一个不错的益智。下面是一些对我的作品:

Not sure why this is useful, but it's a nice puzzle. Here is something that works for me:

在旋转一下,这样做:

RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
int w = mainLayout.getWidth();
int h = mainLayout.getHeight();

mainLayout.setRotation(270.0f);
mainLayout.setTranslationX((w - h) / 2);
mainLayout.setTranslationY((h - w) / 2);

ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mainLayout.getLayoutParams();
lp.height = w;
lp.width = h;
mainLayout.requestLayout();

和布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/main"
    android:layout_height="match_parent"
    android:background="#ffcc88"
    tools:context=".TestRotateActivity" >

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        />

    <Button 
        android:id="@+id/rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rotate"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

这篇关于安卓:旋转整个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆