zoukankan      html  css  js  c++  java
  • 【转】如何在 Android 程序中禁止屏幕旋转和重启Activity

    原文网址:http://www.cnblogs.com/bluestorm/p/3665890.html

    禁止屏幕随手机旋转变化

    有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变化:
    在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android:screenOrientation=”landscape” 属性。

    landscape = 横向
    portrait = 纵向

    避免在转屏时重启Activity

    android中每次屏幕方向切换时都会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次 Create的时候载入配置,那样,进行中的游戏就不会自动重启了!
    要避免在转屏时重启Activity,可以通过在AndroidManifest.xml文件中重新定义方向(给每个Activity加上 android:configChanges=”keyboardHidden|orientation”属性)。
    在需要控制屏幕显示方向的Activity中重写 onConfigurationChanged(Configuration newConfig)方法,这样在转屏时就不会重启Activity了。

    复制代码
    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
        //横向
        setContentView(R.layout.file_list_landscape);
    } else {
        //竖向
        setContentView(R.layout.file_list);
    }
    复制代码
    复制代码
    <activity android:name="com.myapp.MyActivity"
              android:label="@string/app_name"
              android:screenOrientation="landscape"
              android:configChanges="orientation"
              >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    复制代码

    android:screenOrientation=”landscape”
    android:configChanges=”keyboardHidden|orientation”

    复制代码
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
     
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
            //横向
            setContentView(R.layout.file_list_landscape);
        } else {
            //竖向
            setContentView(R.layout.file_list);
        }
    }
    复制代码

    在模拟器中可以按 CTL+F11 模拟做屏幕旋转。

    参考:

    How to disable Screen Auto-Rotation on Android
    http://digitaldumptruck.jotabout.com/?p=897

    如何在 Android 程序中禁止屏幕旋转和重启Activity
    http://www.androidcn.com/news/20110302/00001299.html

  • 相关阅读:
    sql server 中各个系统表的作用==== (转载)
    后台动态设置前台标签内容和属性
    利用C#编写一个简单的抓网页应用程序
    如何创建和使用Web Service代理类
    jdbc如何取得存储过程return返回值
    子窗口和父窗口的函数或对象能否相互访问 (转载)
    把aspx文件编译成DLL文件
    C#中的类型转换
    c#中对文件的操作小结
    转贴一篇 自定义数据库 希望对你有帮助
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4840612.html
Copyright © 2011-2022 走看看