动态改变actionbar上menu的图标

工作中遇到了一个需求是,在滚动的时候让actionbar上的图标进行变色。实现后在这里总结下思路。

动态改变actionbar上menu的图标

一、先在主题中定义好Actionbar的style

 <style name="AppTheme" parent="BaseTheme">

        <item name="actionBarStyle">@style/NAActionBarStyle</item>
<item name="actionBarSize">@dimen/actionbar_height</item>
<item name="actionBarItemBackground">@drawable/action_bar_selector</item> <item name="actionOverflowButtonStyle">@style/MyOverFlow.Dark</item>
<item name="homeAsUpIndicator">@drawable/action_bar_icon_back_dark</item> </style>

二、分区域进行改变

2.1 改变最左边的返回按钮

getSupportActionBar().setHomeAsUpIndicator(R.drawable.action_bar_icon_back_light);

2.2 改变文字的颜色和透明度

这个比较复杂,首先要定义这样一个类:

package com.duitang.main.util;

import android.graphics.Color;
import android.os.Parcel;
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.text.style.ForegroundColorSpan; /**
* Created by YlorD on 14-12-16.
* @author YlorD
* @since v3.7
* from Cyril Mottier
*/
public class AlphaForegroundColorSpan extends ForegroundColorSpan { private float mAlpha; public AlphaForegroundColorSpan(int color) {
super(color);
} public AlphaForegroundColorSpan(Parcel src) {
super(src);
} @Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setColor(getAlphaColor());
} public float getAlpha() {
return mAlpha;
} public void setAlpha(float alpha) {
mAlpha = alpha;
} private int getAlphaColor() {
int foregroundColor = getForegroundColor();
return Color.argb((int) (mAlpha * 255), Color.red(foregroundColor),
Color.green(foregroundColor), Color.blue(foregroundColor));
}
}

然后在代码中动态调用actionbar的setTitle方法就行。

    private AlphaForegroundColorSpan mAlphaForegroundColorSpan;

    private SpannableStringBuilder mSpannableStringBuilder;
mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(getResources().getColor(R.color.dark));
    private void setTitleAlpha(float alpha) {
mAlphaForegroundColorSpan.setAlpha(alpha);
mSpannableStringBuilder.setSpan(mAlphaForegroundColorSpan, 0,
mSpannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(mSpannableStringBuilder);
}

2.3 改变最右边的menu图标

    MenuItem moreItem;

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
moreItem = menu.add(Menu.NONE, Menu.FIRST, Menu.FIRST, null);
moreItem.setIcon(R.drawable.action_bar_icon_more);
moreItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return super.onCreateOptionsMenu(menu);
}

动态改变

 if (moreItem != null) {
moreItem.setIcon(R.drawable.action_bar_icon_more_light);
}

2.4 改变最右边的OverFlow

如果是多个menuItem聚合在了一起,那么就会显示三个小点。怎么动态改变这三个小点的图标呢?

在style中定义好overFlow的样式:

<style name="MyOverFlow.Dark" parent="@style/Widget.AppCompat.Light.ActionButton.Overflow">
<item name="android:src">@drawable/action_bar_icon_more_dark</item>
<item name="android:contentDescription">@string/accessibility_overflow</item>
</style>

这里面的关键点在于写了一个contentDescription,作为overFlow的标识。然后在代码中写上如下的代码:

private TintImageView overflow;
      // The content description used to locate the overflow button
final String overflowDesc = getString(R.string.accessibility_overflow);
// The top-level window
final ViewGroup decor = (ViewGroup) getWindow().getDecorView();
// Wait a moment to ensure the overflow button can be located
decor.post(new Runnable() {
@Override
public void run() {
// The List that contains the matching views
final ArrayList<View> outViews = new ArrayList<>();
// Traverse the view-hierarchy and locate the overflow button
decor.findViewsWithText(outViews, overflowDesc, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
// Guard against any errors
if (outViews.isEmpty()) {
return;
}
// Do something with the view
overflow = (TintImageView) outViews.get(0);
}
});

之后就可以在代码中动态的改变了:

        if (overflow != null) {
overflow.setImageResource(R.drawable.action_bar_icon_more_light);
}

参考自:http://*.com/questions/22046903/changing-the-android-overflow-menu-icon-programmatically

上一篇:jquery动态改变div宽度和高度


下一篇:关于Android开发中的证书和密钥等问题