我正在创建一个动态更改状态栏颜色的应用。
从任何片段调用时,我在主Activity类中的方法工作正常。片段放在Activity pager中:
public void setStatusBarColorIfPossible(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setStatusBarColor(color);
}
}
但是,在任何片段中创建并全屏显示的DialogFragment中,调用my方法无效。状态栏立即变黑(在styles.xml中设置),我无法更改它。
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:
// 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>
<color name="colorPrimary">#FFB72020</color>
<color name="colorPrimaryDark">#000</color>
<color name="textColor">#fff</color>
<color name="tabsScrollColor">#fff</color>
</resources>
任何人都可以解释我为什么?
答案 0 :(得分:12)
为了setStatusBarColor,你需要设置标志:FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
&#34;表示此窗口负责绘制系统栏背景的标志。&#34; (Google API等级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);
}
}
答案 1 :(得分:0)
这对我有用。
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Objects.requireNonNull(Objects.requireNonNull(getDialog()).getWindow()).addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Objects.requireNonNull(Objects.requireNonNull(getDialog()).getWindow()).setStatusBarColor(getResources().getColor(R.color.light_green_400));
}
}
答案 2 :(得分:-1)
使用FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS并在getDecorView中更改索引0中的hight子项。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (getDialog().getWindow() != null) {
// Flag
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// DeviceScreenHeight
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int deviceScreenHeight = displayMetrics.heightPixels;
// Change height child At index zero
((ViewGroup) getDialog().getWindow().getDecorView().getRootView()).getChildAt(0).getLayoutParams().height = deviceScreenHeight;
}
}