【问题标题】:ViewCompat.setOnApplyWindowInsetsListener makes Status Bar color disappearViewCompat.setOnApplyWindowInsetsListener 使状态栏颜色消失
【发布时间】:2023-01-28 00:27:15
【问题描述】:

这是我用来检测 Keyboard Height 何时更改的代码。

唯一的问题是当这段代码运行时,Statur Bar 颜色消失并变成白色。

ViewCompat.setOnApplyWindowInsetsListener(this.getWindow().getDecorView(), (v, insets) -> {

        int keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;

        //Do your job here
        Log.d("Keyboard height: ", String.valueOf(keyboardHeight));

        SharedPreferences preferences = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        if (keyboardHeight > 0) {
            bottom.getLayoutParams().height = 0;
            editor.putInt("keyboard_height", keyboardHeight);
        } else {
            bottom.getLayoutParams().height = preferences.getInt("keyboard_height", 500);
        }

        editor.apply();

        return insets;
    });

任何不改变Status Bar颜色的替代代码?

或者在此代码运行后以编程方式重新添加Status Bar颜色的任何方式?

【问题讨论】:

    标签: java android


    【解决方案1】:

    您可以通过返回 WindowInsetsCompat.CONSUMED 而不是 insets 来消耗 insets 调度事件来解决这个问题

    Per documentation:

    通过从 View.onApplyWindowInsets(WindowInsets) 或 onApplyWindowInsets 返回此值,可以在视图层次结构中的 insets 调度期间使用它来停止将 insets 调度到其子级以避免遍历整个视图层次结构。

    一旦应用程序处理了视图层次结构中特定级别上的所有插入,应用程序就应该返回此实例,并且不再需要分派给它的子级以获得更好的性能。

    更新:

    这确实有效,似乎它只是将操作栏推到屏幕顶部,使操作栏比正常情况下更高。但我想我可以在操作栏的顶部添加一些高度和填充来防止这种情况发生。

    好吧,这似乎确实使活动全屏显示,甚至也与导航栏相交。

    现在解决这个问题,而是返回以下内容:

    ViewCompat.onApplyWindowInsets(v, insets)
    
    ViewCompat.setOnApplyWindowInsetsListener(this.getWindow().getDecorView(), (v, insets) -> {
    
            int keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
    
            //Do your job here
            Log.d("Keyboard height: ", String.valueOf(keyboardHeight));
    
            SharedPreferences preferences = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
    
            if (keyboardHeight > 0) {
                bottom.getLayoutParams().height = 0;
                editor.putInt("keyboard_height", keyboardHeight);
            } else {
                bottom.getLayoutParams().height = preferences.getInt("keyboard_height", 500);
            }
    
            editor.apply();
    
            return ViewCompat.onApplyWindowInsets(v, insets);
        });
    

    【讨论】:

    • 这确实有效,似乎它只是将操作栏推到屏幕顶部,使操作栏比正常情况下更高。但我想我可以在操作栏的顶部添加一些高度和填充来防止这种情况发生。
    • 让我检查一下
    • @Peter 谢谢你;请现在检查答案
    • 美丽的!效果很好,我确实必须删除“!!”虽然,我不知道他们为什么在那里。
    • 对不起,我在科特林测试;忘了删除它快乐的编码:)
    猜你喜欢
    • 2017-11-03
    • 2019-11-25
    • 2015-11-20
    • 2011-08-13
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多