Change Status Bar color when entering Contextual Action Mode

寵の児 提交于 2019-11-29 02:18:56

问题


I have an application that uses theme attribute (colorPrimaryDark) to color the Status Bar on Android v21+:

This is working fine. Now, when user long-presses a list item and enters the contextual action mode, I am able to color the CAB bar using attribute actionModeBackground so it looks like this:

So the action bar is gray, which is what I want, but the status bar is still colored using the theme dark color. I don't want that, I want to change it to dark gray or black.

How can I do this? I don't see any theme attribute that would work here.


回答1:


    private int statusBarColor;

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //hold current color of status bar
            statusBarColor = getWindow().getStatusBarColor();
            //set your gray color
            getWindow().setStatusBarColor(0xFF555555);
        }
        ...
    }

    ...

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //return to "old" color of status bar
            getWindow().setStatusBarColor(statusBarColor); 
        }
        ...
    }
});


来源:https://stackoverflow.com/questions/29618470/change-status-bar-color-when-entering-contextual-action-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!