美文网首页
App默认设置竖屏

App默认设置竖屏

作者: Wang_Mr | 来源:发表于2018-04-10 09:44 被阅读0次

    方法① 设置竖屏的方法抽取到BaseActivity的onCreate()方法中

    // 代码设置竖屏
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    但是发现如果手机横屏打开新的页面时会默认转换为竖屏(会有一个横屏转竖屏的显示过程,此现象会出现在锤子手机上而荣耀手机则不会),强制竖屏功能是实现了,但是还是重新走了生命周期方法,此时如果不处理可能会出现bug

    <!-- 需要设置不重走生命周期方法,清单文件添加configChanges属性 -->
    <activity android:name=".MainActivity"
         android:configChanges="orientation|keyboardHidden|screenSize"/>
    

    荣耀、华为手机都支持分屏操作了,为支持可更改分屏窗口大小,需要进行适配

    <!-- 分屏适配 |screenLayout -->
    <activity android:name=".MainActivity"
         android:configChanges="orientation|keyboardHidden|screenSize|screenLayout"/>
    

    方法② 清单文件设置屏幕方向

    <!-- 设置竖屏,为了适应各种手机最好还是添加configChanges属性 -->
    <activity android:name=".MainActivity"
                android:screenOrientation="portrait"
                android:configChanges="orientation|keyboardHidden|screenSize"/>
    

    相关文章

      网友评论

          本文标题:App默认设置竖屏

          本文链接:https://www.haomeiwen.com/subject/pelghftx.html