在AndroidManifest.xml中设置屏幕方向无效。

7
我有一个简单的“Hello World” Android 测试项目。在我的 AndroidManifest.xml 文件中,我已经设置了。
android:screenOrientation="portrait" 
android:configChanges="keyboardHidden|keyboard|orientation">

但是当我调试代码时,变量isLandscape为True,而它应该是False。
boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

我知道我也可以通过代码设置活动方向,但由于某些原因我需要在 XML 中进行设置。我该如何做?
编辑:我的 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活动方法:
@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变量为True,但有时却为False。这有点像一个bug。

1
屏幕方向是否真的改变了,还是只是变量返回了true? - codeMagic
2
你是在activity标签上设置它吧?而不是放在application标签里?http://developer.android.com/guide/topics/manifest/activity-element.html#screen - Ken Wolf
活动屏幕是横向的,而不是我想要的纵向。 - Cuồn Yết
@CuồnYết 奇怪。我刚刚尝试了一下,无法复制这个问题。这个问题在所有设备/模拟器上都存在吗? - Ken Wolf
@W.K.S 謝謝您,但是它沒有效果。 - Cuồn Yết
显示剩余3条评论
2个回答

12

谢谢Ken Wolf,但我把它放在了Activity标签里,而不是Application标签里。 - Cuồn Yết
我不知道是什么问题,从你的描述来看一切似乎都很正常。也许你可以编辑你的帖子并添加你的 manifest.xml 文件?我们需要更多线索 :) - Ken Wolf
@CuồnYết,如果你声明的目标API是13或更高,则还必须声明screeSize - yugidroid
我想到了另外一件事(可能性不大)- 确保没有其他错误阻止您构建应用程序,并且您实际上正在测试最新版本的应用程序。对字符串或其他内容进行小改动,看看是否可以通过测试。 - Ken Wolf

1

除了一件事情外,一切似乎都很好!我不知道这是否适用于您的情况,但您应该阅读文档 :D

如果您的应用程序针对API级别13或更高级别,则必须声明screeSize

如果您的应用程序针对API级别13或更高级别(由minSdkVersion和targetSdkVersion属性声明),则还应声明“screenSize”配置,因为它在设备在纵向和横向方向之间切换时也会发生变化。

请告诉我这是否解决了您的问题。


感谢yugidroid。我的应用程序目标API级别为17。我已添加了“screenSize”配置,但它不起作用。活动仍然处于横向模式。 - Cuồn Yết

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接