全屏显示
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>
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
)
}
}
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)
}
AndroidAutoSize实现自适应
继承的方法参考官网,这里只重点记录下,由于项目横屏全屏显示需要做的特殊处理点
1.横屏显示,BaseActivtiy实现CustomAdapt接口,isBaseOnWidth返回false,getSizeInDp返回设计稿中的高度(DP值)
override fun isBaseOnWidth(): Boolean {
return false
}
override fun getSizeInDp(): Float {
return 417f
}
2.在onWindowFocusChanged中,重新设置屏幕的实际高度和宽度,设置不包含状态栏
AutoSizeConfig.getInstance().isUseDeviceSize = true//包含状态栏的高度的屏幕高度,全屏或者是沉浸式的高度要包含状态栏
AutoSizeConfig.getInstance().screenWidth = DisplayUtil.getRealScreenWidthRelatedInformation()
AutoSizeConfig.getInstance().screenHeight = DisplayUtil.getRealScreenHeightRelatedInformation()
AutoSize.autoConvertDensityOfCustomAdapt(this,this) //重新计算density