问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Android 15 应用适配默认全屏的行为变更及解决方案

创作时间:
作者:
@小白创作中心

Android 15 应用适配默认全屏的行为变更及解决方案

引用
CSDN
1.
https://blog.csdn.net/qq_38666896/article/details/140150454

Android 15系统引入了默认全屏显示的特性,这可能导致应用内容与系统导航栏重叠,影响用户体验。本文将详细介绍如何在不同开发场景下(包括传统Java视图和Compose)处理系统边距,以确保应用内容正确显示。

简介

Android 15(API 级别 35)系统默认采用全屏显示模式,这可能会影响应用的显示效果,导致应用内的导航标题与系统导航栏重叠,无法正常点击上移的内容。

解决方案

检查应用是否需要适配

如果您的应用还没有实现全面屏适配,那么很可能需要进行相应的调整。以下是一些具体的情况分析:

  • 如果您的应用在 Compose 中使用 Material 3 组件(如 TopAppBarBottomAppBarNavigationBar),这些组件会自动处理边衬区,通常不会受到影响。
  • 如果使用 Material 2 组件,需要手动处理边衬区。在 androidx.compose.material 1.6.0 及更高版本中,可以使用 windowInsets 参数为 BottomAppBarTopAppBarBottomNavigationNavigationRail 手动应用边衬区。对于 Scaffold,可以使用 contentWindowInsets 参数。
  • 如果使用视图和 Material 组件,大多数基于视图的 Material 组件(如 BottomNavigationViewBottomAppBarNavigationRailViewNavigationView)可以自动处理边衬区。但使用 AppBarLayout 时,需要添加 android:fitsSystemWindows="true"
  • 对于自定义可组合项,需要手动将边衬区作为内边距应用。如果内容在 Scaffold 内,可以使用 Scaffold 的内边距值。否则,使用 WindowInsets 应用内边距。
  • 如果使用 BottomSheetSideSheet 或自定义容器,需要使用 ViewCompat.setOnApplyWindowInsetsListener 应用内边距。对于 RecyclerView,还需要添加 clipToPadding="false"

传统Java视图适配方案

在创建界面时,需要设置 fitsSystemWindows 属性为 true。如果在 PreferenceScreen 中无法直接配置,可以在 onCreateView 生命周期中进行设置。

public class MainPref extends PreferenceFragmentBase
        implements Preference.OnPreferenceChangeListener {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        Log.d(TAG, "onCreateView: rootView = "+ rootView + ", setFitsSystemWindows=true.");
        if (rootView != null) {
            rootView.setFitsSystemWindows(true);
        }
        return rootView;
    }
}

Compose适配方案

  1. build.gradle 文件中添加必要的依赖:
dependencies {
    implementation "androidx.core:core-ktx:1.6.0"
    implementation "androidx.appcompat:appcompat:1.3.1"
    implementation "androidx.constraintlayout:constraintlayout:2.0.4"
}
  1. 修改 AndroidManifest.xml 中的布局配置,设置 fitsSystemWindows 属性为 true
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/main_layout"
    tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
  1. 修改 Activity 的创建逻辑,处理窗口边距:
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ConstraintLayout mainLayout = findViewById(R.id.main_layout);
    ViewCompat.setOnApplyWindowInsetsListener(mainLayout, (v, windowInsets) -> {
        WindowInsetsCompat insetsCompat = ViewCompat.getRootWindowInsets(v);
        if (insetsCompat != null) {
            Insets insets = insetsCompat.getInsets(WindowInsetsCompat.Type.systemBars());
            Log.d(TAG, "left = " + insets.left + ", top = " + insets.top + 
                ", right = " + insets.right + ", bottom = " + insets.bottom);
            v.setPadding(insets.left, insets.top, insets.right, insets.bottom);
        } else {
            Log.d(TAG, "insetsCompat is null");
        }
        WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(v);
        if (windowInsetsController != null) {
            windowInsetsController.setAppearanceLightStatusBars(true);
        }
        return WindowInsetsCompat.CONSUMED;
    });
}

官方文档

  1. 行为变更说明:以 Android 15 或更高版本为目标平台的应用
  2. 全面屏应用自检步骤:检查应用是否已采用全屏的检查步骤
  3. 配置建议:稳定配置

UI案例

  1. 在Android 14设备上,如果应用没有升级API版本,则不会出现Android 15 SDK全屏显示带来的负面影响。
  2. 当API和系统都是Android 15时,会受到全屏显示的影响,导致top Bar和导航栏异常重合。
  3. 在Android 15设备上采用无边框应用,并应用边衬区,可以避免界面元素被隐藏。

配置说明

如果应用以Android 15或更高版本为目标平台,Configuration将不再排除系统栏。建议使用更合适的替代方案,如ViewGroupWindowInsetsWindowMetricsCalculator

  • Configuration.screenWidthDpscreenHeightDp尺寸不再排除系统栏。
  • Configuration.smallestScreenWidthDp会受到screenWidthDpscreenHeightDp更改的间接影响。
  • 在近方形设备上,对screenWidthDpscreenHeightDp的更改会间接影响Configuration.orientation
  • Display.getSize(Point)会受到Configuration中变更的间接影响。从API级别30开始,此API已被废弃。
  • 从API级别33开始,Display.getMetrics()就一直以这种方式运行。
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号