Android:在onCreate之前设置活动方向,但不在清单中(HDMI插入问题)

Chr*_*ian 2 android orientation screen-orientation android-orientation android-activity

通过使用setRequestedOrientationin ,我可以以编程方式完美地管理方向变化onCreate.

一切正常,直到我插上HDMI线.在此之后,平板电脑"希望"处于横向模式.所以,当我打开一个活动,它显示在首"风景",然后将其显示之后在"肖像",(因为我打电话的setRequestedOrientation(variable_with_orientation_desired_by_the_user)地方variable=ActivityInfo.SCREEN_ORIENTATION_PORTRAITonCreate你可以"看",我的应用程序保持在每一个活动旋转用户打开;

如果我在清单中设置活动的方向,则此问题已部分消失,因为清单中的方向与用户拾取的方向相同.如果它们不同,则会再次开始相同的行为.

我试过这个,但没有成功:

@Override
public void onCreate(final Bundle saved)
{
    setRequestedOrientation(ScreenOrientation);
    super.onCreate(icicle);
}
Run Code Online (Sandbox Code Playgroud)

那么,有什么方法可以告诉android它在创建活动之前必须创建什么样的方向?(但它不能显而易见)

Cro*_*ono 8

我在清单中使用此声明解决了它:

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

在清单中声明的​​每个活动上都有这个问题。

并继续以setRequestedOrientation()编程方式使用来定义onCreate()方法中的横向还是纵向,

希望能帮助到你!


Chr*_*ian 6

我通过在清单文件中将screenOrientation设置为"后面"来解决它:

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

  • 那不是这个方法的目的。请参阅[docs](http://developer.android.com/guide/topics/manifest/activity-element.html#screen)。 (3认同)