zoukankan      html  css  js  c++  java
  • 禁止横屏和竖屏切换

    在某些场合可能需要禁止横屏和竖屏切换,实现这个要求很简单,只要在AndroidManifest.xml里面加入这一行android :screenOrientation="landscape "(landscape 是横向,portrait 是纵向)。不过android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置。在activity加上android:configChanges="keyboardHidden|orientation"属性,就不会重启activity.而是去调用onConfigurationChanged(Configuration newConfig). 这样就可以在这个方法里调整显示方式.

    Java代码
    @Override  

        public void onConfigurationChanged(Configuration newConfig) {   

            try {   

                super.onConfigurationChanged(newConfig);   

                if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {   

                    // land   

                } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {   

                   // port   

                }   

            } catch (Exception ex) {   

         }   

        }  

    上面是我找的资料,上面的操作做完了可以实现屏幕的横竖屏切换 不重新启动Activity 并且可以在方法中对横竖屏进行操作

    我一般采取的方式是在AndroidManifest.xml中的显示的Activity中调整,红色字部分添加后实现手机屏幕的固定显示,设置为横屏就一直是横屏,设置为竖屏就一直是竖屏不会随着你手机的颠倒而改变,个人觉的挺好用的。

    <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name="QRCodeActivity" android:label="@string/app_name"
      android:screenOrientation="landscape" >
       <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
      </activity>

     </application>

  • 相关阅读:
    Objective-C 排序
    Objective-C 工厂方法
    Objective-C 关键字:retain, assgin, copy, readonly,atomic,nonatomic
    iOS UINavigationController的使用
    iOS UITabBarController的使用
    iOS UISearchController的使用
    iOS中延时执行的几种方式的比较和汇总
    iOS适配 旧项目工程在iOS9下不能正常显示
    iOS Block界面反向传值
    iOS Block简介
  • 原文地址:https://www.cnblogs.com/huanglong/p/2118427.html
Copyright © 2011-2022 走看看