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()就一直以这种方式运行。
热门推荐
沟通技巧助夫妻化解危机,线上交流影响婚姻满意度
鲍国安严谨塑造曹操经典,陈建斌创新演绎引争议
废弃光伏板处理:农村环保面临的新挑战
光伏发电安全吗?专家解读光伏电站的辐射真相
万众期待的鱿鱼游戏第二季上线,剧情深度提升引发热议
上线11天破1.26亿,<鱿鱼游戏2>创新高但口碑两极分化
从隔壁早餐店偷学来的麻球做法,配方和技巧也记下来了,真香
黑虎虾版荷兰豆炒虾仁:低脂健康吃货的福音!
荷兰豆的营养密码:从选购到烹饪的全方位指南
从演员到导演再到投资人,陈建斌如何成为新生代影视领军人
陈建斌版曹操惹争议:现代感表演与历史还原度之争
新疆中药资源普查:2471种中药材的发现之旅
双氯芬酸钠缓释胶囊:非甾体抗炎药,用于多种疼痛治疗
赋能大健康产业发展 川渝携手推动新时期护理专业人才高质量培养
人社部健康管理师:大健康从业人员必备证书
医疗补助申请全攻略:从材料准备到成功获批
“星星果”杨桃的营养密码:抗氧化护心,维C含量超柠檬
易舒美教你科学练出完美马甲线
熬夜党必备:科学护肤+食疗,轻松应对肌肤困扰
熬夜党必备:5步肌肤急救法
广州圣心大教堂:打卡中国最大石构哥特式建筑
从林黛玉的咳嗽看情绪与健康:中医调理方案
《无名之辈》:7.94亿票房见证国产小成本电影的口碑逆袭
无名之辈:一场展现小人物尊严的荒诞喜剧
多线叙事与音画配合:《无名之辈》的精妙剪辑艺术
SIGGRAPH 2024:AI电影技术迎来重大突破
离婚在即:心慌怎么办?从容应对的六个实用策略
子女抚养权争取需要哪些证据
人参鸡汤PK人参炖猪脚,谁才是冬季滋补王者?
动物肝脏:维生素宝库的科学解读