Android的全屏显示状态栏改造
最编程
2024-02-21 22:17:26
...
过时的API
//设置默认隐藏虚拟按键,虚拟按键显示后为半透明
protected open fun hideNavigationBarAndFullScreen() {
val flags: Int
// This work only for android 4.4+
flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// This work only for android 4.4+
// hide navigation bar permanently in android activity
// touch the screen, the navigation bar will not show
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) //虚拟按键透明
(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 // hide nav
// bar
or View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
or View.SYSTEM_UI_FLAG_IMMERSIVE)
// flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// | View.SYSTEM_UI_FLAG_IMMERSIVE
// | View.SYSTEM_UI_FLAG_FULLSCREEN;
} else {
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
}
window.decorView.systemUiVisibility = flags
}
上面这一大堆,全都是过时的api,当然也能用
新的方法
theme
<style name="Goscam_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/white</item>
<item name="colorPrimaryDark">@color/white</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textAllCaps">false</item>
<item name="android:textDirection">locale</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:windowLightStatusBar">true</item>
</style>
theme是必不可少的
window
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
window.isNavigationBarContrastEnforced = false
}
window.statusBarColor = Color.TRANSPARENT
window.navigationBarColor = Color.TRANSPARENT
WindowCompat.setDecorFitsSystemWindows(window, true)
设置状态栏颜色,设置底部导航栏颜色
setDecorFitsSystemWindows
函数就是用来替换过时的函数 setSystemUiVisibility
setDecorfitsSystemWindows的解释
/**
* Sets whether the decor view should fit root-level content views for
* {@link WindowInsetsCompat}.
* <p>
* If set to {@code false}, the framework will not fit the content view to the insets and will
* just pass through the {@link WindowInsetsCompat} to the content view.
* </p>
* <p>
* Please note: using the {@link View#setSystemUiVisibility(int)} API in your app can
* conflict with this method. Please discontinue use of {@link View#setSystemUiVisibility(int)}.
* </p>
*
* @param window The current window.
* @param decorFitsSystemWindows Whether the decor view should fit root-level content views for
* insets.
*/
public static void setDecorFitsSystemWindows(@NonNull Window window,
final boolean decorFitsSystemWindows) {
if (Build.VERSION.SDK_INT >= 30) {
Api30Impl.setDecorFitsSystemWindows(window, decorFitsSystemWindows);
} else if (Build.VERSION.SDK_INT >= 16) {
Api16Impl.setDecorFitsSystemWindows(window, decorFitsSystemWindows);
}
}
可以看到函数内部已经做了版本适配
效果
分别是有导航栏和无导航栏的沉浸式效果
推荐阅读
-
Android 优化了 RecyclerView 的图像显示:Glide 在堆中加载批量位图,以便在 RecyclerView 中分块绘制画布,Kotlin (b)
-
高德地图显示和定位的 Android 实现
-
华为状态栏图标替换_教你如何在手机状态栏中显示自己的名字,电池被替换成彩虹色,简直太有趣了...
-
使用 "计算着色器 "处理图像数据后,渲染的纹理-用户界面无法在 Android 上正常显示
-
vivo 设置全屏后,状态栏变黑_手机已启用全屏,但状态栏不显示高清/全屏? 无法使用?
-
虚幻发布 Android 无法在刘海屏手机上全屏显示的问题
-
Android Studio 的新功能--设备镜像--在电脑端显示手机的实时屏幕并对其进行控制。
-
Android 开发中 nodpi、xhdpi、hdpi、mdpi、ldpi 的概念 - 术语和概念 屏幕尺寸 屏幕的物理尺寸,基于屏幕的对角线长度(如 2.8 英寸、3.5 英寸)。 简而言之,安卓系统将所有屏幕尺寸简化为三大类:大、普通和小。 程序可以为这三种屏幕尺寸提供三种不同的布局选项,然后系统会以合适的方式将布局选项呈现到相应的屏幕上,这个过程不需要程序员用代码进行干预。 屏幕纵横比 屏幕的物理长度与物理宽度之比。程序只需使用系统提供的资源分类器 long(长)和 notlong(不长),就能为具有特定长宽比的屏幕提供配制材料。 分辨率 屏幕的像素总数。请注意,分辨率并不意味着长宽比,尽管在大多数情况下,分辨率表示为 "宽度 x 长度"。在安卓系统中,程序一般不直接处理分辨率。 密度 根据屏幕分辨率,沿屏幕宽度和长度排列的像素数量。 密度较低的屏幕在长度和宽度方向上的像素都相对较少,而密度较高的屏幕通常会在同一区域内排列很多甚至非常非常多的像素。屏幕的密度非常重要;例如,一个界面元素(如按钮)的长度和宽度以像素为单位,在低密度屏幕上会显得很大,但在高密度屏幕上就会显得很小。 独立于密度的像素(DIP)是指程序用来定义界面元素的抽象意义上的像素。它作为一个与实际密度无关的单位,帮助程序员构建布局方案(界面元素的宽度、高度和位置)。 与密度无关的像素在逻辑上与像素密度为 160 DPI 的屏幕上的像素大小相同,而 160 DPI 是安卓平台默认的显示设备。在运行时,平台会以目标屏幕的密度为基准,"透明 "地处理所有所需的 DIP 缩放操作。要将与密度无关的像素转换为屏幕像素,可以使用一个简单的公式:像素 = DIP * (密度 / 160)。例如,在 240 DPI 的屏幕上,1 个 DIP 等于 1.5 个物理像素。强烈建议使用 DIP 来定义程序界面的布局,因为这样可以确保用户界面在所有分辨率的屏幕上都能正常显示。 为了简化程序员在面对各种分辨率时的麻烦,也为了让各种分辨率的平台都能直接运行这些程序,Android 平台将所有屏幕以密度和分辨率作为分类方式,分别分为三类:- 三大尺寸:大、普通、小;- 三种不同密度:高(hdpi)、中(mdpi)和低(ldpi)。DPI 表示 "每英寸点数",即每英寸的像素数。如果需要,程序可以为不同的屏幕尺寸提供不同的资源(主要是布局),为不同的屏幕密度提供不同的资源(主要是位图)。除此之外,程序无需对屏幕尺寸或密度进行任何额外处理。执行时,平台会根据屏幕本身的尺寸和密度特性自动加载相应的资源,并将其从逻辑像素(DIP,用于定义界面布局)转换为屏幕上的物理像素。
-
当 uniapp 被打包为 Android 版本时,使用 uni.chooseLocation 在应用程序端显示的地址列表是空白的。
-
PPT缩略图实现一点即可大全屏显示的方法教程