4

我有一个简单的hello world Android测试项目。在我的AndroidManifest.xml中,我已经设置在AndroidManifest.xml中设置screenOrientation不起作用

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

但是当我调试我的代码中,变量isLandscape为True时,应认为是假

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变量为真,但有时它是假。它有点像一个错误。

+1

定义每个Activity请问屏幕方向实际上改变或者仅仅是该变量返回true ? – codeMagic

+2

您正在将它设置在活动标签上吗?不把它放在应用程序标签中? http://developer.android.com/guide/topics/manifest/activity-element.html#screen –

+0

活动屏幕是横向的,而不是像我想要的那样纵向。 –

回答

1

一切似乎都没问题,除了一件事!我不知道是否是您的情况,但您应该阅读documentation:D

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

如果您的应用程序面向API级别13或更高版本(由 的minSdkVersion和targetSdkVersion属性的声明),那么你也应该 申报“屏幕尺寸”的配置,因为它也改变时的画像之间的 设备开关和风景取向。

让我知道它解决了您的问题。

+0

谢谢yugidroid。我的应用程序目标API级别是17.我已经添加了“screenSize”配置,但它不起作用。该活动仍处于风景中。 –

8

请确保您将它放在<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

你需要把它在你manifest.xml

+0

感谢Ken Wolf,但我把它放在活动标签中,而不是应用标签。 –

+0

我不知道那是什么,从你所描述的一切看起来都很好。也许编辑你的文章并添加你的'manifest.xml'?我们需要更多的线索:) –

+0

@CuồnYết,如果你声明的目标API是13或更高,你也必须声明'screeSize'。 – yugidroid