当前位置:   article > 正文

Android APP全屏显示(去掉顶部状态栏和底部虚拟导航栏)以及使用AndroidAutoSize实现自适应_android 设置全屏无状态栏

android 设置全屏无状态栏
全屏显示

1.AndroidManifest设置APP样式android:theme,我项目中的Activity都是继承自Activity,如果是继承自AppCompatActivity,这里样式需要自己定义

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <!-- LAUNCHER 页面不可设置成singleTask模式,如果设置成singleTask,点击home键,再打开app会返回到LAUNCHER界面 -->
        <activity
            android:name=".activity.SplashPage"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.LoginActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="adjustResize"/>

        <meta-data
            android:name="design_width_in_dp"
            android:value="597" />
        <meta-data
            android:name="design_height_in_dp"
            android:value="417" />
    </application>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

2.定义BaseActivity,继承自Activity,重写onWindowFocusChanged

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        AppUtil.navigationBarStatusBar(this, hasFocus)//隐藏状态栏和导航栏以及actionBar
    }
    
    fun navigationBarStatusBar(activity: Activity, hasFocus: Boolean) {
            if (hasFocus) {
                val decorView: View = activity.window.decorView
                decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                )
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.重写继承自BaseActivity的Activity的onCreate方法,在setContentView之前设置window.attributes

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val params = window.attributes
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window.attributes = params
        activityMainBinding = ActivitySplashBinding.inflate(LayoutInflater.from(this))
        setContentView(activityMainBinding.root)
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
AndroidAutoSize实现自适应

继承的方法参考官网,这里只重点记录下,由于项目横屏全屏显示需要做的特殊处理点
1.横屏显示,BaseActivtiy实现CustomAdapt接口,isBaseOnWidth返回false,getSizeInDp返回设计稿中的高度(DP值)

   override fun isBaseOnWidth(): Boolean {
        return false
    }

    override fun getSizeInDp(): Float {
        return 417f
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.在onWindowFocusChanged中,重新设置屏幕的实际高度和宽度,设置不包含状态栏

        AutoSizeConfig.getInstance().isUseDeviceSize = true//包含状态栏的高度的屏幕高度,全屏或者是沉浸式的高度要包含状态栏
        AutoSizeConfig.getInstance().screenWidth = DisplayUtil.getRealScreenWidthRelatedInformation()
        AutoSizeConfig.getInstance().screenHeight = DisplayUtil.getRealScreenHeightRelatedInformation()
        AutoSize.autoConvertDensityOfCustomAdapt(this,this) //重新计算density
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/332369
推荐阅读
相关标签
  

闽ICP备14008679号