【问题标题】:Android: Prevent screen orientation change but still get notified about itAndroid:防止屏幕方向更改,但仍会收到有关它的通知
【发布时间】:2012-03-16 09:03:10
【问题描述】:

我需要将 Activity 的布局保持在纵向模式,但仍会在屏幕方向更改时收到通知,以便更新我的自定义画布。当我通过将这些行放在清单 XML 中来更改屏幕方向时,我已经设法防止 Android 操作系统重新启动我的 Activity 并更改布局:

android:screenOrientation="portrait"
android:configChanges="orientation"

我还知道,我可以通过从 Activity 调用方法 setRequestedOrientation 以编程方式更改屏幕方向。

问题是,当我设置screenOrientation 标志或调用方法setRequestedOrientation 时,根本没有调用方法onConfigurationChanged。当我从清单中删除标志时,它确实会被调用,但随后布局也会更新,这不是我想要的。

有没有办法防止操作系统更改布局但仍能捕捉到配置更改?

谢谢!

【问题讨论】:

    标签: android layout configuration orientation


    【解决方案1】:

    希望this class能解决你的问题。

    【讨论】:

    • 谢谢!看起来很有用,但是当传感器线程一直在运行时,它不会消耗太多的 CPU / 电池吗?我不需要确切的方向角度,只要它是纵向/横向或其反向对应物。
    • 你可能有一个观点。我没有注意到这门课提供了一个确切的角度,这对于你打算做的事情来说似乎确实太多了。很抱歉,但我不知道您还能如何做到这一点。
    • 由于我没有其他答案,并且您的解决方案可用于解决我的问题,尽管不完全按照我想要的方式,我接受您的回答。谢谢!
    【解决方案2】:

    我遇到了同样的问题。帮助我的事情是在我的 mainfest XML 中添加一个词:

    键盘隐藏

    应该是这样的:

    android:configChanges="orientation|keyboardHidden" 
    

    例如在我的清单文件中:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="pl.chris.mypicture"
       android:versionCode="1"
       android:versionName="1.0"
        >
    
        <uses-sdk android:minSdkVersion="10" />
    
        <application
           android:icon="@drawable/ic_launcher"
           android:label="@string/app_name" >
            <activity
               android:name=".MyPictureActivity"
               android:label="@string/app_name"
               android:configChanges="orientation|keyboardHidden"
               android:screenOrientation="portrait" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    【讨论】:

    • 我的意思是,你如何截取定向模式?您如何知道设备处于纵向模式还是横向模式?谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多