2015-09-06 52 views
0

嘗試更改Android操作欄上homeAsUp按鈕的顏色失敗。我需要在運行時執行此操作。這裏是我得到的:嘗試更改ActionBar homeAsUp按鈕的顏色

@Override public boolean onCreateOptionsMenu(Menu menu){ //充氣菜單;這會將項目添加到操作欄(如果存在)。 getMenuInflater()。inflate(R.menu.menu_register_one,menu);

MenuItem homeItem = null; 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
    homeItem = menu.findItem(R.id.home); 
} else { 
    homeItem = menu.findItem(R.id.up); 
} 

if (homeItem == null) { 
    // I always wind up with a null homeItem 
    Log.e(Constants.TAG, "null"); 
} else { 
    Drawable homeIcon = (Drawable) homeItem.getIcon(); 
    homeIcon.mutate().setColorFilter(Color.parseColor(sharedVisualElements.primaryFontColorHexString()), PorterDuff.Mode.SRC_IN); 
    homeItem.setIcon(homeIcon); 
} 

// this part works just fine 
MenuItem nextItem = menu.findItem(R.id.next); 
Drawable newIcon = (Drawable)nextItem.getIcon(); 
newIcon.mutate().setColorFilter(Color.parseColor(sharedVisualElements.primaryFontColorHexString()), PorterDuff.Mode.SRC_IN); 
nextItem.setIcon(newIcon); 

return true; 

}

這總是空狀態的homeItem捲起。我動作條看起來像這樣(兩個箭頭應該是綠色的和相同的大小):

enter image description here

回答

1

試試這個

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha); 
upArrow.setColorFilter(getResources().getColor(R.color.grey), PorterDuff.Mode.SRC_ATOP); 
getSupportActionBar().setHomeAsUpIndicator(upArrow) 
+0

那麼,你的答案將得到滿足,這就是我的建議。祝你好運和tc –

+0

感謝你!它幾乎在那裏,但它使用不推薦的方法,所以我更新了它。 – Alex

0

嘗試改變R.id.homeandroid.R.id.home

0

這是我如何解決這個問題。由於PARTH Bhayani,但他的解決方案中使用過時的方法,而這並不:

Drawable upArrow = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_arrow_back_white_48dp, null); 

upArrow.mutate().setColorFilter(Color.parseColor(sharedVisualElements.primaryFon‌​tColorHexString()), PorterDuff.Mode.SRC_IN); 

getSupportActionBar().setHomeAsUpIndicator(upArrow); 
0

我知道這是舊的,但最好是不要使用可繪製abc_ic_ab_back_mtrl_am_alpha常數由於它們可以在不同的操作系統改變版本和不同的支持庫中。我使用以下命令獲取homeasup按鈕的drawable:

{ 
    Drawable drawable; 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 
     drawable = getSupportDrawableFromAttrIndex(android.support.v7.appcompat.R.styleable.ActionBar_homeAsUpIndicator); 
    } else { 
     drawable = getDrawableFromAttr(android.R.attr.homeAsUpIndicator); 
    } 
    // apply the tint color here before setting to the actionBar 
    actionBar.setHomeAsUpIndicator(drawable); 
} 

Drawable getSupportDrawableFromAttrIndex(int index) { 
    TypedArray ta = getActivity().obtainStyledAttributes(null, 
      android.support.v7.appcompat.R.styleable.ActionBar, 
      android.support.v7.appcompat.R.attr.actionBarStyle, 
      0); 
    Drawable drawable = AppCompatResources.getDrawable(getActivity(), ta.getResourceId(index, 0)); 
    ta.recycle(); 
    return drawable; 
} 

protected Drawable getDrawableFromAttr(int attr) { 
    int[] attrs = {attr}; 
    TypedArray ta = getActivity().obtainStyledAttributes(R.style.YOUR_APP_THEME, attrs); 
    Drawable drawable = ta.getDrawable(0); 
    ta.recycle(); 
    return drawable; 
} 

然後您可以應用色調。