2

is it possible to the add a compound drawable (something like drawableLeft) to the standard action bar title without using a custom view?

Atm i am using a SpannableString for the title (in order to use a custom typeface). If I can't set a compund drawable would it be possible to use an ImageSpan that i could append to my SpannableString?

Thx

4

2 回答 2

1

是的,绝对的。这是一个简单的例子:

    final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
    final TextView abTitle = (TextView) findViewById(abTitleId);
    abTitle.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_icon, 0, 0, 0);
于 2014-04-02T09:16:04.940 回答
0

感谢adneal的回答,但它不适用于android lollipop,我根据他的方法找到了解决方案。它缺乏标准化,但运作良好。

ViewGroup actionViewGroup = (ViewGroup) findViewById(getResources().getIdentifier("action_bar", "id", "android"));
if (actionViewGroup != null && actionViewGroup.getChildCount() > 0) {
    View view = actionViewGroup.getChildAt(0);
    if (view instanceof TextView) {
        TextView tv = (TextView) view;
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_test, 0, 0, 0);
    }
}
于 2015-05-26T08:30:41.047 回答