Ich erstelle eine App, die die Farbe der Statusleiste dynamisch ändert.Statusbarfarbe von DialogFragment setzen
Meine Methode in der Hauptaktivitätsklasse funktioniert einwandfrei, wenn sie von einem beliebigen Fragment aufgerufen wird. Fragmente werden in Aktivität Pager platziert:
public void setStatusBarColorIfPossible(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setStatusBarColor(color);
}
}
jedoch in DialogFragment, die in jedem Fragment und machte Vollbild erstellt wird, meine Methode Aufruf keine Wirkung. Die Statusleiste ist sofort schwarz (wie in styles.xml eingestellt) und kann nicht geändert werden.
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>
Wer kann mir erklären, warum?
Dies bewirkt, dass der Dialog auch Inhalte unter der Navigationsleiste zeichnet. Für diejenigen, die einen BottomSheetDialog verwenden, ist dies besonders problematisch, da die Schaltflächen der Navigationsleiste über dem Dialogfeld platziert werden. – Brian
Finden Sie eine Lösung? Danke @Brian – UserNameYo