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 组件(如
TopAppBar
、BottomAppBar
和NavigationBar
),这些组件会自动处理边衬区,通常不会受到影响。 - 如果使用 Material 2 组件,需要手动处理边衬区。在
androidx.compose.material
1.6.0 及更高版本中,可以使用windowInsets
参数为BottomAppBar
、TopAppBar
、BottomNavigation
和NavigationRail
手动应用边衬区。对于Scaffold
,可以使用contentWindowInsets
参数。 - 如果使用视图和 Material 组件,大多数基于视图的 Material 组件(如
BottomNavigationView
、BottomAppBar
、NavigationRailView
或NavigationView
)可以自动处理边衬区。但使用AppBarLayout
时,需要添加android:fitsSystemWindows="true"
。 - 对于自定义可组合项,需要手动将边衬区作为内边距应用。如果内容在
Scaffold
内,可以使用 Scaffold 的内边距值。否则,使用WindowInsets
应用内边距。 - 如果使用
BottomSheet
、SideSheet
或自定义容器,需要使用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适配方案
- 在
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"
}
- 修改
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>
- 修改
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;
});
}
官方文档
- 行为变更说明:以 Android 15 或更高版本为目标平台的应用
- 全面屏应用自检步骤:检查应用是否已采用全屏的检查步骤
- 配置建议:稳定配置
UI案例
- 在Android 14设备上,如果应用没有升级API版本,则不会出现Android 15 SDK全屏显示带来的负面影响。
- 当API和系统都是Android 15时,会受到全屏显示的影响,导致top Bar和导航栏异常重合。
- 在Android 15设备上采用无边框应用,并应用边衬区,可以避免界面元素被隐藏。
配置说明
如果应用以Android 15或更高版本为目标平台,Configuration
将不再排除系统栏。建议使用更合适的替代方案,如ViewGroup
、WindowInsets
或WindowMetricsCalculator
。
Configuration.screenWidthDp
和screenHeightDp
尺寸不再排除系统栏。Configuration.smallestScreenWidthDp
会受到screenWidthDp
和screenHeightDp
更改的间接影响。- 在近方形设备上,对
screenWidthDp
和screenHeightDp
的更改会间接影响Configuration.orientation
。 Display.getSize(Point)
会受到Configuration
中变更的间接影响。从API级别30开始,此API已被废弃。- 从API级别33开始,
Display.getMetrics()
就一直以这种方式运行。
热门推荐
中国新能源行业投资价值、投资机会、投资策略及投资建议
高考数学选择题4种解法
角色塑造:崩坏:星穹铁道中角色背景故事的创作与演绎
宝宝风寒感冒可以喝什么饮品
2025年高考英语作文热点话题与范文
为何拉赫马尼诺夫要比柴科夫斯基更能代表俄罗斯音乐?
韩媒:AI预测中国晋级世界杯的概率不足30%,韩国则高达99.2%
最全的竹子的种类及名称
奥美拉唑的副作用有哪些
小青龙汤:中医治疗感冒的经典方剂解析
挥别「脸部皱纹」藏住岁月的痕迹:了解皱纹种类、形成原因与破解方式
智能客服大模型训练流程-具体流程步骤详解
一文带你深入了解,高考计分新规则“赋分制”!
MacBook Pro如何安装Homebrew:详细步骤与常见问题解答
中控密封条的安装方法是什么?安装过程中需要注意哪些细节?
如何拒绝精神内耗,找到内心的平静
毛巾什么材质的好?毛巾怎么选择质量好的?
2025年斋月糖友须知
随身WiFi收费方式解析:流量与时间的选择与建议
小产权房拆迁如何补偿
用Web如何配网
脑转移潜在机制!哈尔滨医科大学:揭示抑制肺腺癌脑转移新策略
Steam双人游戏推荐:30款适合好友或伴侣共度的游戏时光
Steam同屏联机精选:十款必玩双人游戏推荐
狗狗忠诚的表现有哪些
汽车修理工劳动合同内容及相关法律规定
常用色环配色法 色环配色方案
战略管理三大核心:决策、执行、监控
一百斤带皮花生能出多少斤油?影响出油率的关键因素
污泥龄调整方法与排泥量计算指南