码迷,mamicode.com
首页 > 其他好文 > 详细

动态改变actionbar上menu的图标

时间:2015-06-29 19:52:26      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享

 

一、先在主题中定义好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://stackoverflow.com/questions/22046903/changing-the-android-overflow-menu-icon-programmatically

 

动态改变actionbar上menu的图标

标签:

原文地址:http://www.cnblogs.com/tianzhijiexian/p/4607727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!