【问题标题】:setting screenOrientation in AndroidManifest.xml does not work在 AndroidManifest.xml 中设置 screenOrientation 不起作用
【发布时间】:2013-06-26 17:46:47
【问题描述】:

我有一个简单的 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。这有点像一个错误。

【问题讨论】:

  • 屏幕方向是否真的改变了,或者只是变量返回了 true?
  • 你是在活动标签上设置的吧?不把它放在应用程序标签中? developer.android.com/guide/topics/manifest/…
  • 活动屏幕是横向的,而不是我想要的纵向。
  • @KenWolf 是的。我将它设置在活动标签中。
  • @CuồnYế 很奇怪。我真的只是尝试过这个并且无法复制这个问题。所有设备/模拟器都有这个问题?

标签: android xml android-activity orientation landscape-portrait


【解决方案1】:

确保将其放在&lt;activity&gt; 标记中,而不是&lt;application&gt; 标记中。

它只适用于&lt;activity&gt;标签,但如果你把它放在&lt;application&gt;标签中就不会报错。

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

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

您需要为您在manifest.xml 中定义的每个Activity 添加它

【讨论】:

  • 感谢 Ken Wolf,但我把它放在活动标签中,而不是应用程序标签中。
  • 我不知道那是什么,从你的描述来看一切似乎都很好。也许编辑您的帖子并添加您的manifest.xml?我们需要更多线索:)
  • @CuồnYết,如果您声明的目标 API 为 13 或更高版本,您也必须声明 screeSize
  • 我想到的另一件事(远景) - 确保没有其他错误阻止您构建,并且您实际上正在测试最新版本的应用程序。对字符串或其他东西做一个小改动,看看是否能通过。
【解决方案2】:

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

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

如果您的应用程序针对 API 级别 13 或更高级别(如 minSdkVersion 和 targetSdkVersion 属性),那么你也应该 声明“screenSize”配置,因为它也会改变 设备在纵向和横向之间切换。

告诉我这可以解决您的问题。

【讨论】:

  • 感谢 yugidroid。我的应用程序目标 API 级别为 17。我添加了“screenSize”配置,但它不起作用。活动仍处于横向状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 2021-07-25
  • 2013-07-10
  • 2018-03-29
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多