简体   繁体   English

Android更改Trusted Web Activity的StatusBarColor

[英]Android Change StatusBarColor of Trusted Web Activity

I build based on this repository my first TWA / PWA App. 我基于此存储库构建了我的第一个TWA / PWA应用程序。 Everything is working fine, but i can´t change the color of the statusbar. 一切工作正常,但是我无法更改状态栏的颜色。

I modify this file and add this line in the <style> tag: 我修改了这个文件,并在<style>标签中添加了这一行:

<item name="android:statusBarColor">@color/ic_launcher_background</item>

The thing is... it works on the first init of the App well... But 500ms after first init it starts the webview and the statusBarColor is white again. 问题是...它可以很好地在应用程序的第一个init上运行...但是在第一次init之后500毫秒,它会启动webview,并且statusBarColor再次变为白色。

Any idea how i can fix this? 知道我该如何解决吗?

Updated : The latest version of the Support Library ( e849e45c90 ) has been updated to make it easier to change the status bar color. 已更新 :支持库( e849e45c90 )的最新版本已更新,可以更轻松地更改状态栏颜色。

The 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 . SVGOMG示例已更新为可以使用,并且可以在此pull request中看到使应用程序运行所需的必要更改。

The section below is outdated, but leaving here for historical context 以下部分已过时,但此处保留历史背景

It is possible to change the status bar color by customising it when opening the Custom Tabs Intent. 打开“自定义标签意图”时可以通过自定义状态栏颜色来进行更改。

This is not currently configurable in the manifest, and the main way to do it is: 目前无法在清单中对其进行配置,而主要方法是:

  1. Copy the LauncherActivity from the support library repo into your project. LauncherActivity从支持库存储库复制到您的项目中。
  2. change the reference in the AndroidManifest.xml to your copy of the implementation. 将AndroidManifest.xml中的引用更改为实现的副本。
  3. Tweak your LauncherActivity code to setup the status bar, by replacing the getCustomTabsIntent method with something like the code below: 通过将getCustomTabsIntent方法替换为以下代码,来调整LauncherActivity代码以设置状态栏:
    protected CustomTabsIntent getCustomTabsIntent(CustomTabsSession session) {
        return new CustomTabsIntent.Builder(session)
          .setToolbarColor(Color.parseColor("#FF0000"))
          .build();
    }

The code above will create a red status bar. 上面的代码将创建一个红色状态栏。 Replace #FF0000 with the desire color. #FF0000替换为所需的颜色。

try to add this code on onResume() 尝试在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);
    }
}

You should add new meta-data on your AndroidManifest where have you declared Trusted Web Activity (android.support.customtabs.trusted.STATUS_BAR_COLOR) 您应该在已声明“受信任的网络活动” (android.support.customtabs.trusted.STATUS_BAR_COLOR)的 AndroidManifest上添加新的元数据。

 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM