模态底部 sheet 更改状态栏颜色

Modal bottom sheet changes status bar color

标准 BottomSheetDialogFragment 将我的状态栏颜色更改为这种丑陋的绿色,我无法将其更改为任何其他颜色。试过 this 但它不起作用。 有什么想法吗?

2: Status bar screenshot

这是我的对话框class:

public class BottomSheetExample extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.bottom_sheet, container, false);
        // Code below only changes status bar color to black after bottom
        // sheet closes, not while it's open like it should
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.colorBlack, null));
            }
            else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.colorBlack));
                }
            }
        }
        return v;
    }
}

这是 bottom_sheet 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bottom Sheet Example" />
</LinearLayout>

我这样从 MainActivity 调用它:

BottomSheetDialogFragment dialogFragment = new BottomSheetExample();
dialogFragment.show(getSupportFragmentManager(), "tag");

我建议在 Activity 的主题中设置 backgroundDimEnabled,这会使 Activity 后面的 window 变暗,就像显示 Dialog 时一样.尽管我们的 Activity 只是在底部偷看,但它基本上仍然占据了整个屏幕。我们不想给用户留下可以与后面的 Activity 交互的印象,所以让我们调暗它。

values/styles.xml

<style name="AppTheme.Translucent" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>

你的 XML 文件

<View
    android:id="@+id/touch_outside"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<FrameLayout
    android:id="@+id/bottom_sheet"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior".../>

主要Activity

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    setStatusBarDim(true);
    setContentView(R.layout.activity_user);
    findViewById(R.id.touch_outside).setOnClickListener(v -> finish());
    BottomSheetBehavior.from(findViewById(R.id.bottom_sheet))
      .setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
          switch (newState) {
            case BottomSheetBehavior.STATE_HIDDEN:
              finish();
              break;
            case BottomSheetBehavior.STATE_EXPANDED:
              setStatusBarDim(false);
              break;
            default:
              setStatusBarDim(true);
              break;
          }
        }
        ...
    });
  }

  private void setStatusBarDim(boolean dim) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      getWindow().setStatusBarColor(dim ? Color.TRANSPARENT :
        ContextCompat.getColor(this, getThemedResId(R.attr.colorPrimaryDark)));
    }
  }

  private int getThemedResId(@AttrRes int attr) {
    TypedArray a = getTheme().obtainStyledAttributes(new int[]{attr});
    int resId = a.getResourceId(0, 0);
    a.recycle();
    return resId;
  }
}

我使用以下方法解决了问题:

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);