在AndroidManifest.xml中设置screenOrientation不起作用 [英] setting screenOrientation in AndroidManifest.xml does not work

查看:2844
本文介绍了在AndroidManifest.xml中设置screenOrientation不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Hello World Android的测试项目。在我的Andr​​oidManifest.xml中,我已经设置

I have a simple hello world Android testing project. In my AndroidManifest.xml, I already set

android:screenOrientation="portrait" 
android:configChanges="keyboardHidden|keyboard|orientation">

但是当我调试我的code,可变isLandscape为True时,应认为是假

but when I debug my code, the variable isLandscape is True when it should supposed to be False

boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

我知道我还可以设置活动取向code,但我需要将其设置在XML因某些原因。我该怎么办呢?

I know that I can also set the activity orientation by code, but I need to set it in xml for some reasons. How can I do it?

编辑:我的Andr​​oidManifest.xml

my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidgames.mreater"
android:versionCode="1"
android:versionName="1.0" 
android:installLocation="preferExternal">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:icon="@drawable/ic_launcher"
    android:allowBackup="true"
    android:label="Mr. Eater" >
    <activity
        android:name="com.androidgames.mreater.MrEaterGame"
        android:label="Mr. Eater" 
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的实际的onCreate活动方式:

my actual onCreate activity method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    int frameBufferWidth = isLandscape ? 480 : 320;
    int frameBufferHeight = isLandscape ? 320 : 480;
   Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

    float scaleX = (float) frameBufferWidth
            / getWindowManager().getDefaultDisplay().getWidth();
    float scaleY = (float) frameBufferHeight
            / getWindowManager().getDefaultDisplay().getHeight();

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);
    fileIO = new AndroidFileIO(getAssets());
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = this.getStartScreen();
   setContentView(renderView);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
}

现在它变得更怪异的isLandscape变量是真实的,但有时它是假。这在某种程度上就像一个错误。

Now it's getting more weird, the isLandscape variable is True, but sometimes it is False. It's somehow like a bug.

推荐答案

请确保你把它放在&LT;活性GT; 标签,而不是&lt;应用&GT; 标记

Make sure you put it in the <activity> tag, not the <application> tag.

这仅适用于&LT;活性GT; 标签,但如果你把它放在&LT不会抱怨;用途&gt; 标记。

It only works in the <activity> tag, but will not complain if you put it in the <application> tag.

<一个href="http://developer.android.com/guide/topics/manifest/application-element.html">http://developer.android.com/guide/topics/manifest/application-element.html

VS

<一个href="http://developer.android.com/guide/topics/manifest/activity-element.html">http://developer.android.com/guide/topics/manifest/activity-element.html

您需要把它的每个活动您在定义的manifest.xml

You will need to put it for each Activity you define in your manifest.xml

这篇关于在AndroidManifest.xml中设置screenOrientation不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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