【问题标题】:Color of animated ActionBarDrawerToggle in ActionBarActionBar 中动画 ActionBarDrawerToggle 的颜色
【发布时间】:2014-12-02 13:41:49
【问题描述】:

如何使用 AppCompat-v7 (v21) 更改 ActionBar 中动画 ActionBarDrawerToggle 图标的颜色? 我试过这个,但不起作用。谢谢。

myActionBarActivity.java

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mLeftDrawer = (LinearLayout) findViewById(R.id.left_drawer);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.string.drawer_open, R.string.drawer_close)

values/styles.xml

    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/black</item>
    </style>

更新: 已解决,我的主题中有此条目 &lt;item name="actionBarWidgetTheme"&gt; 可能与 &lt;item name="drawerArrowStyle"&gt; 重叠。我删除了它,现在一切正常。

【问题讨论】:

    标签: android-5.0-lollipop android-appcompat


    【解决方案1】:

    这应该可行..

    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
            <item name="spinBars">true</item>
            <item name="color">@color/your_color</item>
        </style>
    

    在你的父主题中..

    <style name="AppTheme" parent="AppBaseTheme">
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
            .
            .
            .
    </style>
    

    【讨论】:

    • 使用工具栏上的 setNavigationOnClickListener 处理您的抽屉
    • 样式 sn-p 与我尝试过的相同。不工作。谢谢。
    • 您使用的是操作栏还是工具栏,支持 v4 中的 ActionBarToggle 在 v21 中已弃用。它现在是从 appcompat v7 导入的。您导入正确的了吗??
    • 我正在使用ActionBarActivity的ActionBar。
    • 我发布的代码对我有用..我的父主题是 Theme.AppCompat.Light,它对我来说效果很好..我有一个红色的抽屉图标和一个旋转的红色箭头我的项目
    【解决方案2】:

    由于支持库 24.1.0,您现在可以访问 getDrawerArrowDrawable()。所以要改变颜色,你只需要这样做:

    toggle.getDrawerArrowDrawable().setColor(getColor(R.color.colorPrimary));
    

    【讨论】:

      【解决方案3】:

      还有一种方法可以通过利用DrawerArrowDrawable 中现有的颜色方法以编程方式进行。您只需将自己的DrawerArrowDrawable 传递给ActionBarDrawerToggle 并保留对DrawerArrowDrawable 的引用,以便调用其方法。

      但是,ActionBarDrawerToggle 中的构造函数当前具有包私有访问权限,因此您需要在 android.support.v7.app 包中添加一个类才能访问它。

      这是一种从ActionBarDrawerToggle 扩展来公开颜色方法的方法。

      把这个类放到/src/android/support/v7/app/:

      /*
       * Licensed under the Apache License, Version 2.0 (the "License");
       * you may not use this file except in compliance with the License.
       * You may obtain a copy of the License at
       *
       *      http://www.apache.org/licenses/LICENSE-2.0
       *
       * Unless required by applicable law or agreed to in writing, software
       * distributed under the License is distributed on an "AS IS" BASIS,
       * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       * See the License for the specific language governing permissions and
       * limitations under the License.
       */
      
      package android.support.v7.app;
      
      import android.app.ActionBar;
      import android.app.Activity;
      import android.content.Context;
      import android.graphics.ColorFilter;
      import android.graphics.drawable.Drawable;
      import android.os.Build;
      import android.support.annotation.ColorInt;
      import android.support.annotation.StringRes;
      import android.support.v4.widget.DrawerLayout;
      import android.support.v7.graphics.drawable.DrawerArrowDrawable;
      import android.support.v7.widget.Toolbar;
      import android.util.Log;
      
      /*
       * Version of ActionBarDrawerToggle that exposes DrawerArrowDrawable's color methods.
       * Needs to be in android.support.v7.app in order to access a constructor in ActionBarDrawerToggle
       * which currently has package-private access.
       */
      public class ColorableActionBarDrawerToggle extends ActionBarDrawerToggle {
      
          private final DrawerArrowDrawable mSlider;
      
          /**
           * Construct a new ColorableActionBarDrawerToggle.
           *
           * <p>The given {@link Activity} will be linked to the specified {@link DrawerLayout} and
           * its Actionbar's Up button will be set to a custom drawable.
           * <p>This drawable shows a Hamburger icon when drawer is closed and an arrow when drawer
           * is open. It animates between these two states as the drawer opens.</p>
           *
           * <p>String resources must be provided to describe the open/close drawer actions for
           * accessibility services.</p>
           *
           * @param activity                  The Activity hosting the drawer. Should have an ActionBar.
           * @param drawerLayout              The DrawerLayout to link to the given Activity's ActionBar
           * @param openDrawerContentDescRes  A String resource to describe the "open drawer" action
           *                                  for accessibility
           * @param closeDrawerContentDescRes A String resource to describe the "close drawer" action
           *                                  for accessibility
           */
          public ColorableActionBarDrawerToggle( Activity activity
                                               , DrawerLayout drawerLayout
                                               , @StringRes int openDrawerContentDescRes
                                               , @StringRes int closeDrawerContentDescRes ) {
              this(activity, null, drawerLayout, null, openDrawerContentDescRes, closeDrawerContentDescRes);
          }
      
          /**
           * Construct a new ColorableActionBarDrawerToggle with a Toolbar.
           * <p>
           * The given {@link Activity} will be linked to the specified {@link DrawerLayout} and
           * the Toolbar's navigation icon will be set to a custom drawable. Using this constructor
           * will set Toolbar's navigation click listener to toggle the drawer when it is clicked.
           * <p>
           * This drawable shows a Hamburger icon when drawer is closed and an arrow when drawer
           * is open. It animates between these two states as the drawer opens.
           * <p>
           * String resources must be provided to describe the open/close drawer actions for
           * accessibility services.
           * <p>
           * Please use {@link #ActionBarDrawerToggle(Activity, DrawerLayout, int, int)} if you are
           * setting the Toolbar as the ActionBar of your activity.
           *
           * @param activity                  The Activity hosting the drawer.
           * @param toolbar                   The toolbar to use if you have an independent Toolbar.
           * @param drawerLayout              The DrawerLayout to link to the given Activity's ActionBar
           * @param openDrawerContentDescRes  A String resource to describe the "open drawer" action
           *                                  for accessibility
           * @param closeDrawerContentDescRes A String resource to describe the "close drawer" action
           *                                  for accessibility
           */
          public ColorableActionBarDrawerToggle( Activity activity
                                               , DrawerLayout drawerLayout
                                               , Toolbar toolbar
                                               , @StringRes int openDrawerContentDescRes
                                               , @StringRes int closeDrawerContentDescRes ) {
              this(activity, toolbar, drawerLayout, null, openDrawerContentDescRes, closeDrawerContentDescRes);
          }
      
          /**
           * In the future, [Google] can make this constructor public if [they] want to let developers customize
           * the animation.
           * 
           * [If they make this constructor public, ColorableActionBarDrawerToggle could be in another package.]
           */
          <T extends DrawerArrowDrawable & DrawerToggle> 
          ColorableActionBarDrawerToggle( Activity activity
                                        , Toolbar toolbar
                                        , DrawerLayout drawerLayout
                                        , T slider
                                        , @StringRes int openDrawerContentDescRes
                                        , @StringRes int closeDrawerContentDescRes ) {
              super( activity
                   , toolbar
                   , drawerLayout
                   , (slider == null) ? slider = (T)new DrawerArrowDrawableToggle(activity, getActionBarThemedContext(activity, toolbar)) 
                                      : slider
                   , openDrawerContentDescRes
                   , closeDrawerContentDescRes );
      
              mSlider = slider;
          }
      
          /*
           * Condensed (equivalent) version of the code in all the getActionBarThemedContext methods 
           * called by ActionBarDrawerToggle constructor as of 23.1.
           */
          private static Context getActionBarThemedContext(Activity activity, Toolbar toolbar) {
              Context context = null;
              if (toolbar != null) {
                  context = toolbar.getContext();
              } else if (activity instanceof DelegateProvider) { // Allow the Activity to provide an impl
                  final Delegate delegate = ((DelegateProvider) activity).getDrawerToggleDelegate();
                  context = delegate.getActionBarThemedContext();
              } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                  final ActionBar actionBar = activity.getActionBar();
                  if (actionBar != null) {
                      context = actionBar.getThemedContext();
                  } else {
                      context = activity;
                  }
              }
              return context;
          }
      
          /// Expose DrawerArrowDrawable's color methods
      
          public void setColorFilter(ColorFilter colorFilter) {
              if(mSlider != null) {
                  mSlider.setColorFilter(colorFilter);
              }
          }
      
          public void setColor(@ColorInt int color) {
              if(mSlider != null) {
                  mSlider.setColor(color);
              }
          }
      
          @ColorInt 
          public int getColor() {
              if(mSlider != null) {
                  return mSlider.getColor();
              }
              return 0;//Color.TRANSPARENT;
          }
      
          public void setAlpha(int alpha) {
              if(mSlider != null) {
                  mSlider.setAlpha(alpha);
              }
          }
      }
      

      然后只需使用ColorableActionBarDrawerToggle 代替ActionBarDrawerToggle,并调用其setColor()setColorFilter() 方法在运行时更改颜色:

      ColorableActionBarDrawerToggle mDrawerToggle;
      // ....
      mDrawerToggle.setColor(0xffff0000); // make it red
      

      【讨论】:

      猜你喜欢
      • 2015-01-05
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多