2016-12-06 233 views
0

我试图改变我在对话框中显示的ActionBar上的“向上”图标的颜色。ActionBar“向上”按钮颜色变化

基本上,我在ActionBar上有一个蓝色的背景。一旦我点击搜索图标,我将背景更改为灰色,这也是我希望我的向上箭头也变成灰色(前一种情况下为白色,背景为蓝色)。

下面是代码当我改变搜索的背景点:

MenuItemCompat.setOnActionExpandListener(searchView, 
new MenuItemCompat.OnActionExpandListener() { 
     @Override 
     public boolean onMenuItemActionExpand(MenuItem menuItem) { 
        mIsSearchViewOpen = true; 
        ActionBar ab = ((MyActivity) getActivity()).getSupportActionBar(); 
        //Change the ActionBar background color when SearchView is expanded 
        if (ab != null) { 
         ab.setDisplayShowHomeEnabled(true); 
         ab.setDisplayHomeAsUpEnabled(true); 
         ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_gray_rect)); 
         ab.setHomeAsUpIndicator(R.drawable.ic_back_arrow); //this doesn't work. its still white. 
        } 
        return true; 
       } 
     }); 

在上述情况下,如所预期的灰色背景施加但为什么未示出的返回箭头?它仍然是白色的。

编辑:我注意到的一件事是,它不适用于该项目展开,但适用于搜索后崩溃。所以,当对话框打开时,它的白色。当我点击搜索扩展它时,它仍然是白色的。但是当我碰到这个,搜索崩溃时,它变成了灰色。

编辑: 设置我的行动吧,我MyActivity喜欢如下:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    if(toolbar == null) { 
     ActionBar ab = getSupportActionBar(); 
     if (ab != null) { 
      ab.setDisplayHomeAsUpEnabled(true); 
      ab.setElevation(0); 
     } 
    } else { 
     setSupportActionBar(toolbar); 
} 

而且,这里是我的toolbar

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    app:elevation="0dp"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:contentInsetLeft="72dp" 
     android:contentInsetStart="72dp" 
     app:contentInsetLeft="72dp" 
     app:contentInsetStart="72dp" 
     app:popupTheme="@style/AppTheme.PopupOverlay" 
     app:theme="@style/MyApp.ThemeOverlay.AppCompat.ActionBar" 
     app:titleTextAppearance="@style/TextAppearance.MyApp.ActionBar.Title" 
     tools:ignore="UnusedAttribute"> 

     <ImageView 
      android:id="@+id/toolbar_logo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/logo_action_bar" 
      android:visibility="gone" 
      tools:ignore="ContentDescription" /> 

     <Spinner 
      android:id="@+id/spinner_nav" 
      style="@style/Widget.MyApp.Spinner.DropDown.ActionBar" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

    </android.support.v7.widget.Toolbar> 

</android.support.design.widget.AppBarLayout> 

这是我的theme,我申请到我的工具栏以上:

<style name="MyApp.ThemeOverlay.AppCompat.ActionBar" parent="Theme.AppCompat"> 
     <item name="android:homeAsUpIndicator">@drawable/ic_arrow_back</item> 
     <item name="homeAsUpIndicator">@drawable/ic_arrow_back</item> 
    </style> 

回答

1

您可以添加一个ColorFilterDrawable进行着色它:

 MenuItemCompat.setOnActionExpandListener(searchView, 
      new MenuItemCompat.OnActionExpandListener() { 
       @Override 
       public boolean onMenuItemActionExpand(MenuItem menuItem) { 
        mIsSearchViewOpen = true; 
        ActionBar ab = ((DSActivity) getActivity()).getSupportActionBar(); 
        //Change the ActionBar background color when SearchView is expanded 
        if (ab != null) { 
         ab.setDisplayShowHomeEnabled(true); 
         ab.setDisplayHomeAsUpEnabled(true); 
         ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_gray_rect)); 
         Drawable drawable = ResourcesCompat.getDrawable(getActivity(), R.drawable.ic_back_arrow, null); 
         drawable.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.gray), PorterDuff.Mode.SRC_IN)); 
         ab.setHomeAsUpIndicator(drawable); 
        } 
        return true; 
       } 
      }); 
+0

当菜单搜索项目展开时,它不适用。它在崩溃后适用。任何想法为什么? – Intern

+0

如何在此代码之外设置ActionBar? –

+0

也许你应该添加类似于onMenuItemActionCollapse() –

0

您清单的活动宣言,​​确保您指定的主题类似如下:

android:theme="@style/MyApp.ThemeOverlay.AppCompat.ActionBar" 
1

添加以下代码改变颜色在setcretentview下面的oncreate方法中向上箭头。

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha); 
    upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP); 
    getSupportActionBar().setHomeAsUpIndicator(upArrow);