Android AppCompat v7 r21更改ActionBar的Alpha';书名

Android AppCompat v7 r21更改ActionBar的Alpha';书名,android,android-actionbar,android-support-library,android-appcompat,Android,Android Actionbar,Android Support Library,Android Appcompat,使用AppCompat的早期版本,很容易获取ActionBar的标题文本视图并对其进行修改 以下是我使用的方法: private TextView getActionBarTitleView() { int id = getResources().getIdentifier("action_bar_title", "id", "android"); return (TextView) findViewById(id); } 然后更改标题的alpha值: getAction

使用AppCompat的早期版本,很容易获取
ActionBar
的标题文本视图并对其进行修改

以下是我使用的方法:

 private TextView getActionBarTitleView() {
     int id = getResources().getIdentifier("action_bar_title", "id", "android");
     return (TextView) findViewById(id);
}
然后更改标题的alpha值:

getActionBarTitleView().setAlpha(ratio*255);
现在由于某些原因,最新AppCompat版本的“action\u bar\u title”不再有效。当我尝试使用我的方法时,它返回我“null”。尝试使用
操作栏的其他id,但没有找到好的id

我在Ahmed Nawara的StackOverflow上看到了一篇帖子,他目前找到的唯一方法是在
工具栏的子视图上进行迭代,每当找到
TexView
时,将其文本值与Toolbar.getTitle()进行比较,以确保这就是我们正在查看的
TexView


如果有人能帮我把这个解决方案集成到我的案例中,因为我不知道怎么做。

我想你的方法是从让ActionBar不无聊开始的。如果你仔细看一看,他详细介绍了另一种技术,以设置动作栏标题的阿尔法灵感来自西里尔·莫蒂埃

它使用一个自定义的
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);
}

希望能有帮助。如果还不够清楚,请再看一下Flavinent Laurent的帖子

我猜你的方法是从让ActionBar不无聊开始的。如果你仔细看一看,他详细介绍了另一种技术,以设置动作栏标题的阿尔法灵感来自西里尔·莫蒂埃

它使用一个自定义的
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);
}

希望能有帮助。如果还不够清楚,请再看一下Flavinent Laurent的帖子

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

在活动中,一旦创建,您将拥有:

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;
}

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

在活动中,一旦创建,您将拥有:

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;
}

你在使用AppCompat工具栏吗?不,我在使用SupportActionBar,你为什么不使用工具栏?当您使用AppCompat 21支持库时,我认为您应该:()到目前为止,我没有尝试使用工具栏,但它似乎可以解决您使用AppCompat工具栏的问题?不,我使用的是SupportActionBar,您为什么不使用工具栏?当您使用AppCompat 21支持库时,我想您应该:()到目前为止,我没有尝试使用工具栏,但它似乎是一个解决方案。非常感谢,它工作起来很有魅力!我很想给你投票,但我在这方面没有足够的声誉。@LinkQuid没问题,很高兴我帮了忙:)非常感谢,它很有魅力!我想给你投票,但我在这方面没有足够的声誉。@LinkQuid没问题,很高兴我帮了忙:)