Android全屏黑边解决方案

在Android12以上的手机,设置全屏后屏幕底部有黑边或者白边,有的屏幕顶部有黑边。解决方案很简单,在使用的主题中添加对应的设置即可,如下:

  1. res/values/themes.xml

    xml 复制代码
    <resources>
        <style name="Base.Theme.Laughf" parent="Theme.Material3.DayNight.NoActionBar">
            <!--全屏后顶部状态栏将不显示-->
            <item name="android:windowFullscreen">true</item>
    
            <!--使屏幕顶部的状态栏透明-->
            <item name="android:windowTranslucentStatus">true</item>
    
            <!--使屏幕底部的导航栏透明,有的手机是半透明-->
            <item name="android:windowTranslucentNavigation">true</item>	        
        </style>
    
        <style name="Theme.Laughf" parent="Base.Theme.Laughf" />
    </resources>
  2. res/values-v27/themes.xml

    xml 复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="Base.Theme.Laughf" parent="Theme.Material3.DayNight.NoActionBar">
            <!--全屏后顶部状态栏将不显示-->
            <item name="android:windowFullscreen">true</item>
    
            <!--使屏幕顶部的状态栏透明-->
            <item name="android:windowTranslucentStatus">true</item>
    
            <!--使屏幕底部的导航栏透明,有的手机是半透明-->
            <item name="android:windowTranslucentNavigation">true</item>
    
            <!--在我的小米11pro(android13) 上,虽然状态栏隐藏了,但是原状态位置上会被显示黑色。用下面的属性可解决,但是它是在API27才出的,所以需要声明在v27的文件夹中-->
            <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
        </style>
    </resources>
  3. 使用主题

    xml 复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <application
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"        
            android:theme="@style/Theme.Laughf">
            <activity
                android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>

    这里把主题应用到了application,则所有的界面都会全屏,也可单独把主题设置到需要全屏的Activity上。

这种全屏后有个问题,如果在界面的底部有按钮,且系统启用了虚拟导航栏,则我们界面的按钮可能显示在虚拟导航栏的下方,这样导致点击不了下面的按钮。这种情况可以看看怎么获取虚拟导航栏的高度,然后给界面设置一个marginBottom。

相关推荐
GISer_Jing1 小时前
跨平台Hybrid App开发实战指南
android·flutter·react native
新之助小锅3 小时前
java版连接汇川PLC,发送数据,读取数据,保持重新链接,适用安卓
android·java·python
2501_916007474 小时前
iOS性能调试工具终极指南,从系统底层到多端协同的全方位优化实践(2025版)
android·ios·小程序·https·uni-app·iphone·webview
2501_915921434 小时前
iOS崩溃日志深度分析与工具组合实战,从符号化到自动化诊断的完整体系
android·ios·小程序·uni-app·自动化·cocoa·iphone
执念WRD7 小时前
熊海CMS v1.0代码审计实战
android·nginx·安全·web安全·网络安全·系统安全
jllllyuz7 小时前
基于ThinkPHP实现动态ZIP压缩包的生成
android
百***920210 小时前
【MySQL】MySQL库的操作
android·数据库·mysql
2501_9160088912 小时前
没有源码如何加密 IPA 实战流程与多工具组合落地指南
android·ios·小程序·https·uni-app·iphone·webview
2501_9400940212 小时前
PS1模拟器 DuckStation更新最新版整合 下载即玩 附PS1Bios/游戏/金手指 安卓版+电脑版
android·游戏·电脑
橙武低代码15 小时前
业务流低代码平台:从理念到实战
android·低代码·ai编程