【问题标题】:Modal bottom sheet changes status bar color模态底部工作表更改状态栏颜色
【发布时间】:2017-07-12 18:22:48
【问题描述】:

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

2:Status 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");

【问题讨论】:

    标签: android android-statusbar


    【解决方案1】:

    我解决了这个问题:

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

    【讨论】:

      【解决方案2】:

      我建议在Activity 的主题中设置backgroundDimEnabled,这会使Activity 后面的窗口变暗,就像显示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".../>
      

      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;
        }
      }
      

      【讨论】:

      • 我知道,但这不是问题所在。问题是模式 BottomSheetDialogFragment 在它处于活动状态时会更改它。我需要它与屏幕的其余部分一样呈深色,而不是像我发布的屏幕截图中那样呈蓝色/绿色。
      • 然后呢?能否请您详细说明一下,我想我也有解决方法:)
      • 我编辑了我之前的评论并附有详细解释。
      • 兄弟,请给我你的代码。这个问题似乎很离奇
      • 尝试了您编辑的答案,但它不起作用。我用相关的代码清单编辑了我的问题。
      猜你喜欢
      • 1970-01-01
      • 2016-10-25
      • 2015-05-09
      • 2015-09-26
      • 2016-11-04
      • 2017-02-02
      • 2021-11-05
      • 2018-08-27
      相关资源
      最近更新 更多