在AndroidManifest.xml中设置screenOrientation不起作用

Cuồ*_*Yết 6 xml android orientation landscape-portrait android-activity

我有一个简单的hello world Android测试项目.在我的AndroidManifest.xml中,我已经设置好了

android:screenOrientation="portrait" 
android:configChanges="keyboardHidden|keyboard|orientation">
Run Code Online (Sandbox Code Playgroud)

但是当我调试我的代码时,变量isLandscape是真的,它应该是假的

boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
Run Code Online (Sandbox Code Playgroud)

我知道我也可以通过代码设置活动方向,但出于某些原因我需要在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>
Run Code Online (Sandbox Code Playgroud)

我的实际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");
}
Run Code Online (Sandbox Code Playgroud)

现在它越来越奇怪了,isLandscape变量是True,但有时它是False.它有点像一个bug.

Ken*_*olf 11

确保将其放在<activity>标签中,而不是<application>标签中.

它仅适用于<activity>标签,但如果将其放入<application>标签中则不会抱怨.

http://developer.android.com/guide/topics/manifest/application-element.html

VS

http://developer.android.com/guide/topics/manifest/activity-element.html

你需要把它放在Activity你定义的每一个中manifest.xml


yug*_*oid 1

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

您必须声明screeSize您的应用程序是否以 API 级别 13 或更高级别为目标。

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

让我知道这可以解决您的问题。