manifest.xml中的屏幕方向和值

Kos*_*din 18 android screen orientation

我希望以横向或纵向使用我表单中的所有活动.当用户选择方向时 - 这对所有活动都有效.尝试"后面"选项方向.根据谷歌的定位将取决于以前的活动.我的第一个活动使用setRequestedOrientation来设置从用户方向中选择的内容.接下来的活动必须遵循相同的方向.我是否也必须在其代码中加入setRequestedOrientation?或者依赖于表现中的'后面'参数?设置setRequestedOrientation可能会导致onCreate再次出现?

更新:尝试"肖像"和setRequestedOrientation() - resilt si onCreate被调用2次.问题出现在下一个活动中 - >因为第一个活动中的"肖像" - android以相同的方向开始下一个活动.它忽略了由我设定的"风景"方向:(

Raj*_*ran 22

如果您想为活动定位,那么您可以使用 -

机器人:screenOrientation = "画像"

android:screenOrientation="sensorPortrait"
Run Code Online (Sandbox Code Playgroud)

作为该清单中该活动的属性.但是,如果要在启动应用程序时根据先前的方向设置方向运行时,则需要检查以前的方向onCreate(),然后使用编程方式将其设置为该值setRequestedOrientation()

更新: @ s.co.tt指出android:screenOrientation="sensorPortrait"用于更好地支持平板电脑.

有关android:screenOrientation它们的不同值和每个值的更多详细信息,请查看文档:

https://developer.android.com/guide/topics/manifest/activity-element.html#screen

  • 不要使用`肖像'.在某些平板电脑(可能还有其他设备)上,您的活动会颠倒显示,因为这些设备认为这是正确的默认纵向方向.`sensorPortrait`(或者在较旧的API中,拼写错误的`sensorPortait`)将仅以纵向显示您的内容,但会将其旋转以匹配用户持有设备的方式. (7认同)

小智 7

<activity
            android:name=".Android_mobile_infoActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  </activity>
Run Code Online (Sandbox Code Playgroud)


San*_*ans 5

要处理方向的变化,请在标签 下的AndroidManifest.xml: 中添加以下行,如下所示:-android:configChanges="orientation|screenSize"<activity>

   <application
        android:allowBackup="true"
        ----------
        ---------- >
        <activity android:name=".MainActivity"
            android:configChanges="orientation|screenSize">
        ----------
        </activity>
        ----------
    </application>
Run Code Online (Sandbox Code Playgroud)

注意:每当配置发生变化时,例如orientationscreenLayoutscreenSize等)时,活动都会重新启动并onCreate调用其方法。为了防止这种情况,我们必须处理配置中的任何更改。


Kos*_*din 0

现在我在 onCreate 中进行了检查:

m_bSkip = (this.getRequestedOrientation() != MyApp.sInstance.GetScreenOrientation());
if (m_bSkip)
  return;
Run Code Online (Sandbox Code Playgroud)

当我输入 oncreate 并且不需要屏幕方向时 - 退出。当我进入 onCreate 并且需要屏幕方向时 - 继续初始化。这修复了这种情况,无需保留与活动相关的异步任务并检查新活动。当然,所有函数:onStart、onResume、onPausde、onStop...都必须检查此标志以避免空指针异常。