模态底部工作表更改状态栏颜色

时间:2017-07-12 18:22:48

标签: android android-statusbar

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

2Status bar screenshot

这是我的对话类:

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");

2 个答案:

答案 0 :(得分:2)

我通过使用:

解决了这个问题
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

答案 1 :(得分:0)

我建议在backgroundDimEnabled的主题中设置Activity,这会使Activity后面的窗口变暗,就像显示Dialog时一样。即使我们的Activity仅在底部偷看,它仍然基本上占据整个屏幕。我们不希望留下用户可以与活动进行交互的印象,所以让我们调暗它。

<强>值/ 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".../>

<强> MainActivity

  @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;
  }
}
相关问题