Files
zhini_im/状态栏颜色修改的解决方案.txt
rw0067680 c01808ac21 first commit
Change-Id: Ib7c2ab10a2562044fcaf9879388a6cbc1db6ac61
2025-12-23 10:00:49 +08:00

77 lines
3.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
一、目标
- 解决应用顶部出现“灰色状态栏”与页面顶栏不一致的问题,实现类似微信的“状态栏与页面顶部一体化”。
- 要求:背景色一致、状态栏图标/文字颜色(黑/白)可按页面动态切换,内容不被状态栏遮挡。
二、适配范围
- Android 6.0+API 23可切换状态栏图标深浅色。
- 低于 6.0 的机型只支持设置状态栏背景色,不支持图标颜色切换(系统限制)。
三、核心实现
1) 复用基类的状态栏控制能力
- 基类:`uikit/src/main/java/cn/wildfire/chat/kit/WfcBaseActivity.java`
- 方法:`setTitleBackgroundResource(int resId, boolean dark)`
- 作用:同步设置 Toolbar 背景、状态栏背景色,并切换状态栏图标深浅(通过 `setStatusBarTheme`)。
- 参数含义:
- `resId`颜色资源ID
- `dark`true 表示状态栏图标为浅色适合深色背景false 表示状态栏图标为深色(适合浅色背景)。
2) SettingActivity 动态设置为白色一体化
- 文件:`app/src/main/java/com/xunpaisoft/social/im/setting/SettingActivity.java`
- 位置:`afterViews()`
- 增加调用:
`setTitleBackgroundResource(R.color.white, false);`
含义:状态栏与顶栏背景统一为白色,状态栏图标为深色(黑色)。
3) 布局根视图避免内容被状态栏遮挡
- 文件:`app/src/main/res/layout/setting_activity.xml`
- 修改:
- 根布局背景:`@color/white`
- 启用沉浸适配:`android:fitsSystemWindows="true"`
示例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true"
android:orientation="vertical"/>
四、使用指引(推广到其他页面)
- 浅色顶栏(白底):
`setTitleBackgroundResource(R.color.white, false);`
- 深色顶栏(如深蓝/黑):
`setTitleBackgroundResource(R.color.colorPrimary, true);`
- 对应页面的根布局需:
`android:fitsSystemWindows="true"`,或在根布局顶部增加与状态栏等高的 padding。
五、常见问题与排查
1) 仍有“灰条”或色差:
- 检查页面根布局或父容器是否设置了与顶栏不一致的背景色(例如 `gray5`)。
- 检查是否是继承了带背景的通用容器(如 `ProgressFragment`),移除其硬编码背景或与页面顶栏统一。
2) 内容被状态栏遮挡/上移:
- 确认根布局已加 `android:fitsSystemWindows="true"`。
- 如页面使用沉浸全屏且自定义标题栏,需手动加上 `statusBarHeight` 的顶部 padding。
3) 某些 ROMMIUI、ColorOS图标颜色未切换
- Android 6.0 以下系统不支持切换图标颜色,属系统限制。
- 个别 ROM 存在兼容差异,可考虑厂商私有 Flag可按需再扩展
六、验证步骤
1) 启动应用:
`adb shell am start -n com.xunpaisoft.social/.im.main.MainActivity`
2) 在应用内进入“设置”页,观察:
- 顶部状态栏与页面顶栏背景均为纯白,无灰条;
- 状态栏图标/时间为深色;
- 列表内容不被遮挡,可正常滚动。
七、变更清单
- `app/src/main/res/layout/setting_activity.xml`
- 根布局背景设为 `@color/white`
- 增加 `android:fitsSystemWindows="true"`
- `app/src/main/java/com/xunpaisoft/social/im/setting/SettingActivity.java`
- `afterViews()` 中调用 `setTitleBackgroundResource(R.color.white, false)`
八、后续扩展
- 如需按页面动态切换,只需在各 Activity/Fragment 对应时机调用 `setTitleBackgroundResource(colorRes, isDarkIcons)`。
- 对 Fragment 场景,建议由承载 Activity 统一设置状态栏,并确保 Fragment 根布局同样处理 `fitsSystemWindows` 或顶部 padding。