Ale*_*mer 1 android android-appcompat android-actionbar android-support-library
使用以前版本的AppCompat,很容易获得ActionBar标题TextView并对其进行修改.
有我使用的方法:
private TextView getActionBarTitleView() {
int id = getResources().getIdentifier("action_bar_title", "id", "android");
return (TextView) findViewById(id);
}
Run Code Online (Sandbox Code Playgroud)
然后更改标题的alpha值:
getActionBarTitleView().setAlpha(ratio*255);
Run Code Online (Sandbox Code Playgroud)
现在由于某些原因,使用最新的AppCompat版本的"action_bar_title"不再起作用了.当我尝试使用我的方法时,它返回"null".试图使用其他的id,ActionBar但我找不到好的.
我在Ahmed Nawara的StackOverflow上看到了1个帖子,他目前找到的唯一方法是对子Toolbar视图进行迭代,每当TexView找到a时,将它的文本值与toolbar.getTitle()进行比较,以确保在TexView我们看到的.
如果有人可以帮助我将这个解决方案整合到我的案例中,因为我实际上不知道该怎么做.
Mat*_*ree 10
我猜你从Flavien Laurent的帖子中得到了你的方法,让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));
}
}
Run Code Online (Sandbox Code Playgroud)
然后,使用a 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);
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.如果不够清楚,再看看Flavient Laurent的帖子吧!
| 归档时间: |
|
| 查看次数: |
2345 次 |
| 最近记录: |