AppCompat v7 r21改变了ActionBar标题的Alpha

时间:2014-10-26 10:50:13

标签: android android-actionbar android-support-library android-appcompat

使用以前版本的AppCompat,很容易获得ActionBar的标题TextView并对其进行修改。

我使用的方法是:

 private TextView getActionBarTitleView() {
     int id = getResources().getIdentifier("action_bar_title", "id", "android");
     return (TextView) findViewById(id);
}

然后更改标题的alpha值:

getActionBarTitleView().setAlpha(ratio*255);

现在由于某些原因,使用最新AppCompat版本的“action_bar_title”不再有效。当我尝试使用我的方法时,它返回“null”。试图使用ActionBar的其他id,但我找不到好的。

我在Ahmed Nawara的StackOverflow上看到了1个帖子,他目前找到的唯一方法是对Toolbar的子视图进行迭代,每当找到TexView时,比较它使用toolbar.getTitle()确定文本值,以确保我们正在查看TexView

如果有人可以帮助我将此解决方案整合到我的案例中,因为我实际上不知道该怎么做。

2 个答案:

答案 0 :(得分:10)

我猜你从Flavien Laurent's post获得了让你的ActionBar变得无聊的方法。如果仔细观察一下,他会详细介绍另一种技术来设置由Cyril Mottier启发的ActionBar标题的alpha。

它使用扩展AlphaForegroundColorSpan的自定义ForegroundColorSpan类:

public class AlphaForegroundColorSpan extends ForegroundColorSpan
{
    private float mAlpha;

    public AlphaForegroundColorSpan(int color)
    {
        super(color);
    }

    public AlphaForegroundColorSpan(Parcel src)
    {
        super(src);
        mAlpha = src.readFloat();
    }

    public void writeToParcel(Parcel dest, int flags)
    {
        super.writeToParcel(dest, flags);
        dest.writeFloat(mAlpha);
    }

    @Override
    public void updateDrawState(TextPaint ds)
    {
        ds.setColor(getAlphaColor());
    }

    public void setAlpha(float alpha)
    {
        mAlpha = alpha;
    }

    public float getAlpha()
    {
        return mAlpha;
    }

    private int getAlphaColor()
    {
        int foregroundColor = getForegroundColor();
        return Color.argb((int) (mAlpha * 255), Color.red(foregroundColor), Color.green(foregroundColor), Color.blue(foregroundColor));
    }
}

然后,使用SpannableString,您只需将Alpha设置为AlphaForegroundColorSpan,然后将此AlphaForegroundColorSpan设置为SpannableString

public void onCreate(Bundle savedInstanceState)
{
    ...
    spannableString = new SpannableString("ActionBar title");
    alphaForegroundColorSpan = new AlphaForegroundColorSpan(0xffffffff);
    ...
}

private void setActionBarTitle(int newAlpha)
{
    alphaForegroundColorSpan.setAlpha(newAlpha);
    spannableString.setSpan(alphaForegroundColorSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    getSupportActionBar().setTitle(spannableString);
}

希望它有所帮助。如果不够清楚,再看看Flavient Laurent的帖子吧!

答案 1 :(得分:1)

使用AppCompat,您应该使用新工具栏,包括活动布局中的toolbar.xml并导入android.support.v7.widget.Toolbar。

在您的活动中,OnCreate您将拥有:

mtoolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mtoolbar);
getActionBarTitleView().setAlpha(ratio*255);

此时你已经快完成了,你可以使用反射来访问视图(记得导入java.lang.reflect.field;),你的函数将是:

 private TextView getActionBarTitleView() {
     TextView yourTextView = null;
        try {
          Field f = mToolBar.getClass().getDeclaredField("mTitleTextView");
          f.setAccessible(true);
          yourTextView = (TextView) f.get(mToolBar);
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {
    }
    return yourTextView;
}