Android:旋转整个布局

Android: Rotate whole layout(Android:旋转整个布局)

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

问题描述

我需要能够动态旋转整个布局(单击按钮).

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

我可以使用例如旋转布局.layout.setRotation(270.0f).问题是,旋转后,布局heightwidth不匹配其父级的.

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.

我试过像这样反转 heightwidth

I have tried inverting height and width like so,

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;

什么都不做.

我正在使用 sdk 14.

下面的第一张图片是启动时的应用程序.第二个,轮换后.我想填补黑色的空间".任何帮助将不胜感激.

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:

在旋转点击时,这样做:

On rotate click, do this:

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>

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

本文标题为:Android:旋转整个布局

基础教程推荐