Android 更改受信任网站的 StatusBarColor Activity
Android Change StatusBarColor of Trusted Web Activity
我基于此构建 repository 我的第一个 TWA / PWA 应用程序。一切正常,但我无法更改状态栏的颜色。
我修改了这个 file 并在 <style>
标签中添加了这一行:
<item name="android:statusBarColor">@color/ic_launcher_background</item>
事情是......它在应用程序的第一次初始化时运行良好......但是在第一次初始化后 500 毫秒它启动了 webview 并且 statusBarColor 再次变为白色。
知道我该如何解决这个问题吗?
已更新:
已更新最新版本的支持库 (e849e45c90),以便更轻松地更改状态栏颜色。
SVGOMG sample has been updated to use it, and the necessary changes for apps to make it work can be seen in this pull request.
下面的部分已经过时,但留在这里是为了了解历史背景
可以在打开自定义标签 Intent 时通过自定义来更改状态栏颜色。
目前无法在清单中配置,主要实现方式是:
- 将 LauncherActivity 从支持库 repo 复制到您的项目中。
- 将 AndroidManifest.xml 中的引用更改为您的实施副本。
- 调整您的 LauncherActivity 代码以设置状态栏,方法是将 getCustomTabsIntent 方法替换为类似下面的代码:
protected CustomTabsIntent getCustomTabsIntent(CustomTabsSession session) {
return new CustomTabsIntent.Builder(session)
.setToolbarColor(Color.parseColor("#FF0000"))
.build();
}
上面的代码将创建一个红色状态栏。将 #FF0000
替换为想要的颜色。
尝试在 onResume() 上添加此代码
public void setStatusBarColor(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(color);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
您应该在您的 AndroidManifest 中添加新的元数据,您声明了 Trusted Web Activity (android.support.customtabs.trusted.STATUS_BAR_COLOR)
<activity android:name="android.support.customtabs.trusted.LauncherActivity">
<meta-data
android:name="android.support.customtabs.trusted.DEFAULT_URL"
android:value="https://your-host.com/" />
<meta-data
android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR"
android:resource="@color/colorPrimaryDark" />
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:scheme="https"
android:host="your-host.com"/>
</intent-filter>
</activity>
我基于此构建 repository 我的第一个 TWA / PWA 应用程序。一切正常,但我无法更改状态栏的颜色。
我修改了这个 file 并在 <style>
标签中添加了这一行:
<item name="android:statusBarColor">@color/ic_launcher_background</item>
事情是......它在应用程序的第一次初始化时运行良好......但是在第一次初始化后 500 毫秒它启动了 webview 并且 statusBarColor 再次变为白色。
知道我该如何解决这个问题吗?
已更新: 已更新最新版本的支持库 (e849e45c90),以便更轻松地更改状态栏颜色。
SVGOMG sample has been updated to use it, and the necessary changes for apps to make it work can be seen in this pull request.
下面的部分已经过时,但留在这里是为了了解历史背景
可以在打开自定义标签 Intent 时通过自定义来更改状态栏颜色。
目前无法在清单中配置,主要实现方式是:
- 将 LauncherActivity 从支持库 repo 复制到您的项目中。
- 将 AndroidManifest.xml 中的引用更改为您的实施副本。
- 调整您的 LauncherActivity 代码以设置状态栏,方法是将 getCustomTabsIntent 方法替换为类似下面的代码:
protected CustomTabsIntent getCustomTabsIntent(CustomTabsSession session) {
return new CustomTabsIntent.Builder(session)
.setToolbarColor(Color.parseColor("#FF0000"))
.build();
}
上面的代码将创建一个红色状态栏。将 #FF0000
替换为想要的颜色。
尝试在 onResume() 上添加此代码
public void setStatusBarColor(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(color);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
您应该在您的 AndroidManifest 中添加新的元数据,您声明了 Trusted Web Activity (android.support.customtabs.trusted.STATUS_BAR_COLOR)
<activity android:name="android.support.customtabs.trusted.LauncherActivity">
<meta-data
android:name="android.support.customtabs.trusted.DEFAULT_URL"
android:value="https://your-host.com/" />
<meta-data
android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR"
android:resource="@color/colorPrimaryDark" />
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:scheme="https"
android:host="your-host.com"/>
</intent-filter>
</activity>