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。

相关推荐
陆业聪18 小时前
2026 年还在靠「感觉」调性能?Android Profiler 这样用才对
android·人工智能·性能优化
草莓熊Lotso19 小时前
MySQL 多表连接查询实战:内连接 + 外连接
android·运维·数据库·c++·mysql
草莓熊Lotso19 小时前
Linux 进程信号深度解析(下):信号的保存、阻塞与捕捉
android·linux·运维·服务器·数据库·c++·性能优化
小羊子说1 天前
Android系统中 socketpair 的源码解读与应用分析小结
android·java
FL4m3Y4n1 天前
MySQL索引原理与SQL优化
android·sql·mysql
我命由我123451 天前
Android Gradle - Gradle 自定义插件(Build Script 自定义插件、buildSrc 自定义插件、独立项目自定义插件)
android·java·java-ee·kotlin·android studio·android-studio·android runtime
冬奇Lab1 天前
AudioFlinger混音机制深度解析
android·音视频开发·源码阅读
滑雪的企鹅.1 天前
Kotlin云头条技术点剖析(项目复习02)——用户协议页面
android·开发语言·kotlin
JMchen1231 天前
Android NDK开发从入门到实战:解锁应用性能的终极武器
android·开发语言·c++·python·c#·android studio·ndk开发