试图改变ActionBar homeAsUp按钮的颜色

时间:2015-09-06 17:48:18

标签: android android-actionbar

我尝试更改Android操作栏上homeAsUp按钮的颜色失败了。我需要在运行时这样做。这就是我所拥有的:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    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

4 个答案:

答案 0 :(得分:1)

试试这个

INSERT INTO table1
SELECT "hello" AS field1, 15 AS field2, 1262340000 AS timestamp 
UNION SELECT "byebye", 10, 1262340000 
UNION SELECT "hi", 20, 1262340000 
UNION SELECT "boo", 25, 1262340000 

答案 1 :(得分:0)

尝试将R.id.home更改为android.R.id.home

答案 2 :(得分: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);

答案 3 :(得分:0)

我知道这是旧的,但最好不要使用drawable abc_ic_ab_back_mtrl_am_alpha常量,因为它们可以在不同的OS版本和不同的支持库中进行更改。我使用以下内容来获取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;
}

然后你可以应用色调。