實際上,我想每次更改TabLayout
,StatusBar
和Toolbar
的顏色。正確更改分段中狀態欄,工具欄和TabLaylout的顏色
更改片段,如:
- 片段1:藍色
- fragment2:紅色
- fragment3:黃色
- 片段4:橙色。
顏色正在改變,但不是根據我給出的,它隨機挑選顏色。 我在main中創建了一個方法「changeHeader()」,並在片段中調用該方法。
主要活動方法
public void changeHeader(int toolbarcolor, int statusbarcolor, int tablayoutcolor) {
toolbar.setBackgroundColor(toolbarcolor);
tabLayout.setBackgroundColor(tablayoutcolor);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
getWindow().setStatusBarColor(statusbarcolor);
}
navHeader.setBackgroundColor(tablayoutcolor);
}
撥打片段1
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.blue1),
getResources().getColor(R.color.blue2),
getResources().getColor(R.color.blue3)
);
調用片段2
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.red1),
getResources().getColor(R.color.red2),
getResources().getColor(R.color.red3)
);
在片段3
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.yellow1),
getResources().getColor(R.color.yellow2),
getResources().getColor(R.color.yellow3)
);
調用
在片段4
((Main)getActivity()).changeHeader(
getResources().getColor(R.color.orange1),
getResources().getColor(R.color.orange2),
getResources().getColor(R.color.orange3)
);
我在片段的onCreateView稱爲 「changeHeader」 電話,請爲我提供解決方案。
在'onAttach()'方法中獲取您的活動引用並調用您的'chageHeader()'方法。 – Piyush
是的,如果您確實調用片段onAttach()中的'changeHeader()'方法,它將確保創建片段並將其附加到活動時,它將調用此方法。 –