从DialogFragment设置状态栏颜色 [英] Set status bar color from DialogFragment
问题描述
我正在创建一个可动态更改状态栏颜色的应用程序.
I'm creating an app which changes status bar color dynamically.
从任何片段中调用时,主Activity类中的我的方法都可以正常工作.片段放置在活动"寻呼机中:
My method in main Activity class works fine when called from any fragment. Fragments are placed in Activity pager:
public void setStatusBarColorIfPossible(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setStatusBarColor(color);
}
}
但是,在任何片段中创建并全屏显示的DialogFragment中,调用我的方法无效.状态栏立即变为黑色(如在styles.xml中设置的那样),我无法更改.
However, in DialogFragment which is created in any fragment and made fullscreen, calling my method has no effect. Status bar is instantly black (as set in styles.xml) and I can't change it.
public class AddOrEditWeekPlanDialog extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
}
@Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
d.getWindow().setLayout(width, height);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dialog, container, false);
Button button = (Button)root.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ThermostatActivity) getActivity()).setStatusBarColorIfPossible(0xFF0D47A1);
}
});
return root;
}
@Override
public void onResume() {
super.onResume();
}
}
styles.xml:
styles.xml:
// Set in manifest for main activity
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
// Set in dialog class
<style name="MyDialog" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsFloating">false</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
resources.xml:
resources.xml:
<resources>
<color name="colorPrimary">#FFB72020</color>
<color name="colorPrimaryDark">#000</color>
<color name="textColor">#fff</color>
<color name="tabsScrollColor">#fff</color>
</resources>
任何人都可以解释我为什么?
Anyone can explain me why?
推荐答案
要设置setStatusBarColor,您需要设置标志:FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
In order to setStatusBarColor you need to set the flag: FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
标志,指示此窗口负责绘制系统栏的背景." (Google API级别21)
"Flag indicating that this Window is responsible for drawing the background for the system bars." (Google API level 21)
尝试以下代码:
public void setStatusBarColorIfPossible(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(color);
}
}
这篇关于从DialogFragment设置状态栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!