当使用ActionMode,状态栏将变成黑色的棒棒糖 [英] When using ActionMode, the status bar turns black on Lollipop

查看:1353
本文介绍了当使用ActionMode,状态栏将变成黑色的棒棒糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与它下面的主题集中的状态栏:

 <! - 基本主题为所有材料-esque风格。我们使用NoActionBar
     所以我们可以使用工具栏在运行时。
 - >
<样式名称=材料父=Theme.AppCompat.Light.NoActionBar>
    <项目名称=机器人:windowDrawsSystemBarBackgrounds>真< /项目>
    <项目名称=机器人:windowTranslucentStatus>真< /项目>
    ...
< /风格>
 

我也有一个 DrawerLayout 我的大多数活动,利用这台状态栏的颜色,我喜欢:

  mDrawerLayout.setStatusBarBackgroundColor(getResources()的getColor(R.color.myapp_green)。);
 

我使用的是工具栏,而不是默认的动作条,所以在我的布局存在(即抽屉式导航栏绘制在它的上面)。

一切正常,但在我活动之一,我有一个多选模式下使用 ActionMode 。当此 ActionMode 被激活(使用长preSS),它覆盖了工具栏使用:

 <项目名称=机器人:windowActionModeOverlay>真< /项目>
<项目名称=windowActionModeOverlay>真< /项目>
<项目名称=actionModeStyle> @风格/ Material.Widget.ActionMode< /项目>
 

Material.Widget.ActionMode 的风格是:

 <样式名称=Material.Widget.ActionMode父=@风格/ Widget.AppCompat.ActionMode>
    <项目名称=机器人:背景> @色/ myapp_green< /项目>
    <项目名称=背景> @色/ myapp_green< /项目>
< /风格>
 

现在的问题是,每当出现这种情况,状态条会从 myapp_green 颜色为黑色。这是几乎一样,如果状态栏半透明关闭(我使用的是Android 5.0)。我想知道我怎么可能能够得到这个行为不会发生,并保持状态栏的颜色/半透明原样。

我试图添加<项目名称=机器人:windowTranslucentStatus>真< /项目> 的动作模式的风格,以及添加<项目名称=机器人:statusBarColor> @色/ myapp_green< /项目> 的样式为 ActionMode ,既没有成功。

更新:

我不知道这事做与我设置状态栏背景靠不住的方式。我所有的活动类来源于NavigationDrawerActivity.java:

  / **
 *一个{@link活动},支持导航抽屉,这是一种拉式面板导航
 *菜单。此抽屉从屏幕(右侧上的RTL设备)的左侧拉出。
 * /
公共类NavigationDrawerActivity扩展ActionBarActivity
  实现AdapterView.OnItemClickListener {

  私有静态最后弦乐LOGTAG = NavigationDrawerActivity.class.getSimpleName();

  私人DrawerLayout mDrawerLayout;
  私人的ListView mDrawerList;
  私人LayoutInflater mInflater;
  私人NavigationDrawerItemAdapter mAdapter;
  私人ActionBarDrawerToggle mDrawerToggle;

  私人NavigationDrawerItem [] mNavigationDrawerItems;

  私人工具栏mAppBar;

  @覆盖
  保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
//我们必须调用super.setContentView()这里,因为BaseActivity重新定义的setContentView()
//我们不希望使用它。
super.setContentView(R.layout.navigation_drawer);

mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    setupNavigationDrawer();
  }

  @覆盖
  保护无效onPostCreate(包savedInstanceState){
super.onPostCreate(savedInstanceState);

    //同步发生onRestoreInstanceState后切换状态。
    mDrawerToggle.syncState();
  }

  @覆盖
  公共无效onConfigurationChanged(配置NEWCONFIG){
    super.onConfigurationChanged(NEWCONFIG);
    mDrawerToggle.onConfigurationChanged(NEWCONFIG);
  }

  @覆盖
  公共布尔onCreateOptionsMenu(功能菜单){
    返回true;
  }

  @覆盖
  公共布尔onOptionsItemSelected(菜单项项){
    INT的id = item.getItemId();

    开关(ID){
      案例android.R.id.home:
        返回mDrawerToggle.onOptionsItemSelected(项目);
    }

    返回super.onOptionsItemSelected(项目);
  }

  / **
   *切换导航抽屉的状态(即关闭它,如果它是开放的,如果打开它
   *它关闭)。
   * /
  公共无效toggleNavigationDrawer(){
    如果(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
      closeNavigationDrawer();
    } 其他 {
      openNavigationDrawer();
    }
  }

  / **
   *打开抽屉式导航。
   * /
  公共无效openNavigationDrawer(){
    mDrawerLayout.openDrawer(GravityCompat.START);
  }

  / **
   *关闭导航抽屉。
   * /
  公共无效closeNavigationDrawer(){
    mDrawerLayout.closeDrawer(GravityCompat.START);
  }

  / **
   *初始化特定于导航抽屉的项目。
   * /
  私人无效setupNavigationDrawer(){
    mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerLayout.setStatusBarBackgroundColor(getResources()的getColor(R.color.wiw_green));

        mAppBar =(工具栏)findViewById(R.id.app_bar);
        setSupportActionBar(mAppBar);

        动作条动作条= getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(真正的);
        actionBar.setHomeButtonEnabled(真正的);
        actionBar.setDisplayShowHomeEnabled(假);

        mDrawerToggle =新ActionBarDrawerToggle(
          此时,/ *我们的上下文(活动承载这个抽屉里)* /
          mDrawerLayout,/ *其中资产净值抽屉就会得出的DrawerLayout * /
          R.string.drawer_open,/ *打开的抽屉中,描述了访问* /
          R.string.drawer_close / *的亲密抽屉,描述的可访问* /
        ){

          / **
           *当调用一个抽屉处于完全封闭的状态下稳定运行。
           * /
          公共无效onDrawerClosed(查看视图){
            super.onDrawerClosed(视图);
            supportInvalidateOptionsMenu();
          }

          / **
           *当调用一个抽屉处于完全开放的状态下稳定运行。
           * /
          公共无效onDrawerOpened(查看drawerView){
            super.onDrawerOpened(drawerView);
            supportInvalidateOptionsMenu();
          }
        };

        mDrawerList =(ListView控件)mDrawerLayout.findViewById(R.id.drawer_list);

        mNavigationDrawerItems = buildNavDrawerItemsList();

        setupAdapter(mNavigationDrawerItems);

        setupNavigationDrawerHeader();

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerList.setOnItemClickListener(本);
      }

      @覆盖
      公共无效onItemClick(适配器视图<>母公司,查看诚挚的范例,INT。负责,长期的援助){
        // $ C $不相关Ç
      }

      / **
       *设置这个内部内容查看{@link NavigationDrawerActivity}有一个给定的布局。
       *
       *参数aLayoutId布局的I​​D加载到本次活动的内部内容视图。
       * /
      公共无效setDrawerContent(INT aLayoutId){
        LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup中的root =(ViewGroup中)findViewById(R.id.drawer_content);
        inflater.inflate(aLayoutId,根);
      }
    }
 

其实,我跑 DrawerLayout.setStatusBarBackgroundColor()它有效果。就在值-V21 / styles.xml改变 colorPrimaryDark 对状态栏没有任何影响。我觉得这可能是问题......这些类正在从一个非物质的主题转换成一个新的,材料类主题的根,所以我想知道如果我错过了什么,当我做了转换到 colorPrimaryDark 被正确识别。

解决方案

我张贴的答案,因为在这里,而其他的解决方案是有帮助的,没有人确切地回答了这个问题。我能找出调用 DrawerLayout.setStatusBarBackgroundColor()是造成麻烦。我的解决办法如下:

  1. 启用 windowTranslucentStatus windowDrawsSystemBarBackgrounds 中的主旋律。
  2. 删除调用 DrawerLayout.setStatusBarBackgroundColor()我的 NavigationDrawerActivity ,使所有<$ C $内C>活动类派生。
  3. 将以下样式在我的值-V21 / styles.xml 基本主题:

        @色/ app_green
        @色/ app_green
        @色/ app_green_dark
        @色/ app_green_dark
        ?ATTR / colorPrimaryDark
        ?ATTR / colorPrimaryDark

<醇开始=4>

  • NavigationDrawerActivity 类的,在其的onCreate()的方法,我执行以下命令(感谢@Tinadm对于这部分的答案): getWindow()getDecorView()setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

  • 在我的类presents的 ActionMode ,我增加了以下方法:

  •     @覆盖
        在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
          如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.LOLLIPOP){
            。getActivity()getWindow()clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //这是突出的状态栏和操作栏中的区别,
            //作为操作栏,而在操作模式在有色app_green_dark
            getActivity()getWindow()setStatusBarColor(getResources()的getColor(R.color.app_green_darker))。。;
          }
    
          // 其他的东西...
          返回true;
        }
    
        @覆盖
        公共无效onDestroyActionMode(ActionMode模式){
          mActionMode = NULL;
          如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.LOLLIPOP){
            。getActivity()getWindow()addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getActivity()getWindow()setStatusBarColor(getResources()的getColor(R.color.app_green_dark))。。;
          }
        }
    

    重新启用半透明的状态栏,当 ActionMode 被破坏使我的抽屉式导航栏,状态栏的下方进行绘制。另外值得一提的是, ActionMode 覆盖操作栏,所以它会覆盖在抽屉式导航,如果它退出时, ActionMode 被使能(通过划屏左到右)。我要禁用此功能,当 ActionMode 已启用,所以它不会迷惑用户。

    I have a status bar with the following in the theme set on it:

    <!-- Base Theme for all "Material"-esque styles. We use NoActionBar
         so we can use the Toolbar at runtime.
    -->
    <style name="Material" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        ...
    </style>
    

    I also have a DrawerLayout for most of my activities, which sets the color of the status bar to my liking using:

        mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.myapp_green));
    

    I am using a Toolbar, rather than the default ActionBar, so it exists in my layout (i.e. the navigation drawer draws on top of it).

    Everything works fine, except that in one of my activities, I have a multi-select mode with an ActionMode. When this ActionMode is activated (using a long press), it overlays the Toolbar using:

    <item name="android:windowActionModeOverlay">true</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeStyle">@style/Material.Widget.ActionMode</item>
    

    The Material.Widget.ActionMode style is:

    <style name="Material.Widget.ActionMode" parent="@style/Widget.AppCompat.ActionMode">
        <item name="android:background">@color/myapp_green</item>
        <item name="background">@color/myapp_green</item>
    </style>
    

    Now, the problem is that, whenever this happens, the status bar turns from the myapp_green color to black. It's almost as if the status bar translucency is turned off (I'm using Android 5.0). I'm wondering how I might be able to get this behavior to not happen, and to keep the status bar color/translucency as-is.

    I've tried to add <item name="android:windowTranslucentStatus">true</item> to the action mode's styles, as well as adding <item name="android:statusBarColor">@color/myapp_green</item> in the style for the ActionMode, both without success.

    Update:

    I wonder if this has something to do with the wonky way that I'm setting the status bar background. All of my Activity classes derive from NavigationDrawerActivity.java:

    /**
     * An {@link Activity} that supports a Navigation Drawer, which is a pull-out panel for navigation
     * menus. This drawer is pulled out from the left side of the screen (right side on RTL devices).
     */
    public class NavigationDrawerActivity extends ActionBarActivity
      implements AdapterView.OnItemClickListener {
    
      private static final String LOGTAG = NavigationDrawerActivity.class.getSimpleName();
    
      private DrawerLayout mDrawerLayout;
      private ListView mDrawerList;
      private LayoutInflater mInflater;
      private NavigationDrawerItemAdapter mAdapter;
      private ActionBarDrawerToggle mDrawerToggle;
    
      private NavigationDrawerItem[] mNavigationDrawerItems;
    
      private Toolbar mAppBar;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    // We have to call super.setContentView() here because BaseActivity redefines setContentView(),
    // and we don't want to use that.
    super.setContentView(R.layout.navigation_drawer);
    
    mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        setupNavigationDrawer();
      }
    
      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
      }
    
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
      }
    
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        return true;
      }
    
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        switch(id) {
          case android.R.id.home:
            return mDrawerToggle.onOptionsItemSelected(item);
        }
    
        return super.onOptionsItemSelected(item);
      }
    
      /**
       * Toggles the state of the navigation drawer (i.e. closes it if it's open, and opens it if
       * it's closed).
       */
      public void toggleNavigationDrawer() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
          closeNavigationDrawer();
        } else {
          openNavigationDrawer();
        }
      }
    
      /**
       * Opens the navigation drawer.
       */
      public void openNavigationDrawer() {
        mDrawerLayout.openDrawer(GravityCompat.START);
      }
    
      /**
       * Closes the navigation drawer.
       */
      public void closeNavigationDrawer() {
        mDrawerLayout.closeDrawer(GravityCompat.START);
      }
    
      /**
       * Initializes items specific to the navigation drawer.
       */
      private void setupNavigationDrawer() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.wiw_green));
    
            mAppBar = (Toolbar) findViewById(R.id.app_bar);
            setSupportActionBar(mAppBar);
    
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayShowHomeEnabled(false);
    
            mDrawerToggle = new ActionBarDrawerToggle(
              this,                  /* Our context (Activity that hosts this drawer) */
              mDrawerLayout,         /* The DrawerLayout where the nav drawer will be drawn */
              R.string.drawer_open,  /* Description of "open drawer", for accessibility */
              R.string.drawer_close  /* Description of "close drawer", for accessibility */
            ) {
    
              /**
               * Called when a drawer has settled in a completely closed state.
               */
              public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                supportInvalidateOptionsMenu();
              }
    
              /**
               * Called when a drawer has settled in a completely open state.
               */
              public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                supportInvalidateOptionsMenu();
              }
            };
    
            mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.drawer_list);
    
            mNavigationDrawerItems = buildNavDrawerItemsList();
    
            setupAdapter(mNavigationDrawerItems);
    
            setupNavigationDrawerHeader();
    
            mDrawerLayout.setDrawerListener(mDrawerToggle);
            mDrawerList.setOnItemClickListener(this);
          }
    
          @Override
          public void onItemClick(AdapterView<?> parent, View aView, int aPosition, long aId) {
            // Code not relevant
          }
    
          /**
           * Set the inner content view of this {@link NavigationDrawerActivity} to have a given layout.
           *
           * @param aLayoutId The id of the layout to load into the inner content view of this activity.
           */
          public void setDrawerContent(int aLayoutId) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewGroup root = (ViewGroup)findViewById(R.id.drawer_content);
            inflater.inflate(aLayoutId, root);
          }  
        }
    

    I actually have to run DrawerLayout.setStatusBarBackgroundColor() for it to have an effect. Just changing colorPrimaryDark in values-v21/styles.xml has no effect on the status bar. I feel like this could be the root of the problem... These classes are being converted from a non-Material theme to a new, Material-like theme, so I'm wondering if I missed something when I did the conversion to the colorPrimaryDark to be recognized correctly.

    解决方案

    I'm posting an answer here because, while the other solutions were helpful, none of them answered the question exactly. I was able to find out that the call to DrawerLayout.setStatusBarBackgroundColor() was causing the trouble. My solution is the following:

    1. Enable windowTranslucentStatus and windowDrawsSystemBarBackgrounds in the main theme.
    2. Remove the call to DrawerLayout.setStatusBarBackgroundColor() inside of my NavigationDrawerActivity, from which all Activity classes derive.
    3. Set the following styles in my values-v21/styles.xml base theme:

            @color/app_green
            @color/app_green
            @color/app_green_dark
            @color/app_green_dark
            ?attr/colorPrimaryDark
            ?attr/colorPrimaryDark
    

    1. Inside of the NavigationDrawerActivity class, in its onCreate() method, I execute the following (thanks @Tinadm for this part of the answer): getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

    2. Inside of my class that presents the ActionMode, I added the following methods:

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // This is to highlight the status bar and distinguish it from the action bar,
            // as the action bar while in the action mode is colored app_green_dark
            getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.app_green_darker));
          }
    
          // Other stuff...
          return true;
        }
    
        @Override
        public void onDestroyActionMode(ActionMode mode) {
          mActionMode = null;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.app_green_dark));
          }
        }
    

    Re-enabling the translucent status bar when the ActionMode is destroyed enables my navigation drawer to be drawn underneath the status bar. It's also worth noting that the ActionMode overlays the action bar, so it will overlay the navigation drawer if it's pulled out when the ActionMode is enabled (via swiping left-to-right). I am going to disable this functionality when the ActionMode is enabled, so it doesn't confuse users.

    这篇关于当使用ActionMode,状态栏将变成黑色的棒棒糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆