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.material1.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()就一直以这种方式运行。
热门推荐
设备OEE提升方案:策划思路与案例解读
构建和谐家庭:与儿媳相处的智慧之道
正宗台湾卤肉饭
如何通过年利率计算利息并理解其意义?这种计算方法在实际应用中有哪些注意事项?
东京审判亲历记
域名选择与规划如何影响SEO?如何优化域名以提升搜索引擎排名?
Proxmox(PVE)链路聚合配置指南:增加带宽与实现负载均衡
抵押贷款的定义、适用范围与抵押物
EEG信号时频域分析详解
Gerber文件使用详解
PCB热过孔的优化设计
不同年龄、性别的急性心肌梗死患者hs-cTnI诊断阈值及影响因素分析
情侣两日一夜轻旅行必去!小众景点推荐
从“习得性无助”到“成长型思维”:突破困境,拥抱成长
游戏建模利器盘点,揭秘高效三维建模与渲染软件特点
棒骨补骨脂莴笋汤
补骨脂是什么植物的种子?补骨脂的植物来源是什么?
从零开始学习PCB设计的基础知识
梯子使用与安全培训
读瓷·悦赏▏美的如痴如醉——陶瓷经典蓝釉
湖北:推广微生物技术 促进茶园绿色发展
港股资金转回的操作方法是什么?这种操作方法的风险如何控制?
学校食堂的5款创新菜,学生看后,纷纷表示:宁愿饿死
常见的容器化平台有哪些?它们的特点和适用场景是什么?
商业化竞速:“人造太阳”如何照进现实?
【安全提示】雨天出行别大意,交通安全请牢记!
2型糖尿病的治疗中,二甲双胍,是个重要的“配角”
英语自然拼读法(Phonics)学习步骤详解
小仲马诞辰200周年!“茶花女”的底色
揭秘蜜蜂自然分蜂:时间、条件与过程全解析