加载Admob/Firebase广告后,DayNight主题中的颜色变暗

Mat*_*ski 9 android admob android-theme firebase

我使用Theme.AppCompat.DayNight.NoActionBar主题为我的应用程序.当我加载adMob interstital时,某些颜色会在"夜间"模式下被破坏(即在RecyclerView中).

屏幕:

在此输入图像描述

那些不正确的颜色来自"notnight"值.当我关闭应用程序并再次运行它一切都很好.当我杀了应用程序时,我有同样的情况.

活动代码:

public class MainActivity extends AppCompatActivity {

    static {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_AUTO);
    }

    private ArrayList<String> planetList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

        populateRecycler();

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        PlanetAdapter adapter = new PlanetAdapter(planetList, getApplicationContext());
        recyclerView.setAdapter(adapter);

        InterstitialAd interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId("ca-app-pub-543543543/543543543");
        AdRequest adRequest = new AdRequest.Builder().build();
        interstitialAd.loadAd(adRequest);
    }

    private void populateRecycler() {
        for (int i = 0; i < 20; i++) {
            planetList.add("TEST");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

当我评论interstitialAd.loadAd(adRequest)一切都好.

你可以在这里找到整个项目: github

Roy*_*erg 19

该问题可能是由WebView重置UI模式引起的,这可以通过WebView手动实例化来解决.

这样做后我没有看到这个问题(Application.oncreate()在这个特定的应用程序中):

    if (nightMode != AppCompatDelegate.MODE_NIGHT_NO) {
        Log.d(TAG, "Manually instantiating WebView to avoid night mode issue.");
        try {
            new WebView(getApplicationContext());
        } catch (Exception e) {
            Log.e(TAG, "Got exception while trying to instantiate WebView to avoid night mode issue. Ignoring problem.", e);
        }
    }
    AppCompatDelegate.setDefaultNightMode(nightMode);
Run Code Online (Sandbox Code Playgroud)

来源:https://groups.google.com/forum/#! msg/ google-admob-ads- sdk/ OZzHq_- wAFY/ K50jClZcBAAJ

  • 哇这实际上有效.WTF谷歌? (10认同)
  • 设置暗模式后创建 webview 组件时,我遇到了同样的问题。即使我将 Appcompat 更新到 1.1.0-rc01,问题仍然存在。上述解决方法解决了该问题。 (4认同)
  • 哇,非常感谢你,成功了!我已经搜索了很长时间,但没有成功。我认为你的帖子和谷歌小组论坛中的讨论是关于这个(非常关键和困难)主题/错误的唯一一篇。 (2认同)
  • 这不再是问题,无需继续依赖解决方法。Google 已经解决了这个问题,我们只需将 appcompat 更新到最低值 https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha03 (2认同)