如何改变屏幕方向,而无需创建android上的一个新的活动? [英] How to change screen orientation, without creating a new activity on android?

查看:158
本文介绍了如何改变屏幕方向,而无需创建android上的一个新的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如果标题是正确的,这里是发生了什么。

I do not know if the title is correct, here is what happens.

我有不同的作品在手机上,并在平板电脑上的应用程序,在手机上它显示为在平板电脑上它显示为横向纵向。

I have an application which works differently on a phone and on a tablet, on a phone it shows as portrait on a tablet it shows as landscape.

要做到这一点,我创建了一个名为CoreActivity类,这是由我的所有活动延伸并执行以下操作:

To achieve this I created a class called CoreActivity which is extended by all my activities and does the following:

public class CoreActivity extends Activity {
    protected boolean _landscape = false;

    public boolean isPhone() {
        int layoutSize = getScreenLayoutSize();
        return (layoutSize == Configuration.SCREENLAYOUT_SIZE_SMALL || layoutSize == Configuration.SCREENLAYOUT_SIZE_NORMAL);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isPhone() && !_landscape) {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }


    protected int getScreenLayoutSize() {
        return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
    }
}

当我想在屏幕上显示在横向模式的手机设置中,要做到这一点我用下面的出现我的问题:

My problem occurs when I wish to show a screen on a phone setup on landscape mode, to do this I use the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    _landscape = true
    super.onCreate(savedInstanceState);
}

问题是,在手机上,如果用户持有的手机上的肖像模式(如人愿,由于大多数应用程序是在纵向模式下),那么该活动被创建和销毁,然后重新创建。但是,如果他们都抱着它风景模式,那么仅创建一次。

The problem is, that on a phone, if the user is holding the phone on portrait mode (as one would, since most of the application is in portrait mode) then the activity gets created and destroyed and then recreated. But if they are holding it on landscape mode, then it is only created once.

出现我的问题,因为在onCreate方法我启动该加载数据的线程,我也显示片段。

My problem occurs because on the onCreate method I launch the threads that load data, and I also show fragments.

有没有办法来避免这个问题?有在纵向模式从一开始就推出一个活动,而不是改变它,或者它不能创造两次的方式?

Is there a way to avoid this problem? is there a way to launch an activity from the start on portrait mode and not change it, or have it not create twice?

在此先感谢您的帮助,您可以提供

Thanks in advance for any help you can provide

推荐答案

重新出现,只是因为你可能为了设置屏幕方向强迫,通过调用 setRequestedOrientation 。然而,你并不需要检查和code改变它。您可以通过XML文件做到这一点。用户可以根据屏幕尺寸不同的XML文件。但它是有点劈所以在功能不能保证。

Recreating occurs just because you are forcing to, by calling setRequestedOrientation probably in order to set screen orientation. However you don't need to check and change it by code. You can do it via xml file. You can set different xml files depending on the screen size. But it is little bit hack so there is no guarantee in the feature.

在清单文件可以通过强制

On the manifest file you can force by:

<activity
   android:name="com.my.example.MyActivity"
   android:screenOrientation="landscape"/>  // or portrait

至于象我明白你要强制对小尺寸和横向(或传感器),较大的屏幕尺寸画像。但是,以上配置适用于所有的屏幕尺寸。这里是棘手的部分:景观 纵向 传感器等。都是整数定义<一个href=\"http://developer.android.com/reference/android/content/pm/ActivityInfo.html#SCREEN_ORIENTATION_PORTRAIT\">here.
正如你可能已经猜到,你可以写的android:screenOrientation = 0 而不是景观。我们从开始的Andr​​oid的经验知道,我们可以定义在XML文件中的整数那么他们的价值观可能会在屏幕大小而异。所以..

So as far as I understand you want to force portrait for small sizes and landscape(or sensor) for larger screen sizes. But above configuration applies for all screen sizes. Here is the tricky part: landscape portrait sensor etc. are all integers defined here. As you might guess you can write android:screenOrientation=0 instead of landscape. What we know from beginning lessons of android, we can define integers in xml files then their values might vary on screen size. So..

您应该先创建不同的屏幕大小不同的 integers.xml 文件。即:

You should first create different integers.xml files for different screen sizes. i.e.:

values
 - integers.xml
values-large
 - integers.xml

值/ integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">1</integer>  // 1 for portrait
</resources>

价值观大/ integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">0</integer> // 0 for landscape
</resources>

最后你必须通过操作清单文件:

and finally you you have to manipulate manifest file by:

<activity
   android:name="com.my.example.MyActivity"
   android:screenOrientation="@integer/orientation"/>  // read constant from xml files

您也可以使用传感器 fullSensor nosensor 锁定背后 reverseLandscape reversePortait 选项了。但是,我警告你,这是一个黑客攻击的解决方案。

You can also use sensor , fullSensor, nosensor, locked, behind, reverseLandscape, reversePortait options too. But I am warning you, this is a hack solution.

这篇关于如何改变屏幕方向,而无需创建android上的一个新的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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