2016-10-14 59 views
0

我的問題是我想改變特定片段中狀態欄的顏色。在所有其他片段中,顏色應該是相同的。例如,在所有片段中,我的狀態欄顏色爲紅色。但在特定片段中,我想要將其更改爲黑色。誰能告訴我我該怎麼做?如何更改特定片段中狀態欄的顏色?

這是我的片段onCreateView方法: - 片段

@Override 
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { 
     final View view = (View) inflater.inflate(R.layout.slide_user_image, container, false); 

     Window window = getActivity().getWindow(); 
     window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
     window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
     window.setStatusBarColor(getActivity().getResources().getColor(R.color.black)); 


     return view; 
    } 
+0

最好的部分將是使用主題。 –

回答

2
Window window = getActivity().getWindow(); 
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
    window.setStatusBarColor(getActivity().getResources().getColor(R.color.black)); 

編寫代碼的onResume(),並回覆上的onPause()方法的顏色,所以該應用程序將在舊statusbarcolor其他片段。