2015-09-06 19 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; 
} 

然后您可以应用色调。