setting translucent status bar from a fragment

被刻印的时光 ゝ 提交于 2019-12-23 23:00:20

问题


For Lollipop+, I am trying to set the status bar translucent from a fragment. The calling activity’s layout contains a FrameLayout, the activity uses the SupportFragmentManager to replace the original fragment with the new fragment. Inside the new fragment’s onViewCreated I am using the ContextThemeWrapper to set the new theme. The theme contains several changes and changing the status bar to translucent is one of them. The other theme changes take effect, but the status bar maintains the color from the calling activity’s theme.

Note: If I set the calling activity's theme to the translucent action bar theme, both activity and fragments have translucent status bars, so its not a problem with the theme.


回答1:


In the fragment's onCreateView method, added :

 getActivity().getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
 getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT );

Turns out the changing of theme wasn't necessary if one is just after a translucent status bar.




回答2:


1. Can you try to set setStatusBarColor transparency programmatically

getWindow().setStatusBarColor(Color.TRANSPARENT);

2. Its achieve to add below line in your styles.xml.

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


来源:https://stackoverflow.com/questions/34011779/setting-translucent-status-bar-from-a-fragment

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