如何更改 Actionbar 的 Title/Subtitle 的字体?
How to change the Typeface of the Title/Subtitle of an Actionbar?
我想自定义 ActionBar 的标题和副标题的字体。为此,我将所需的 TTF-file 放在资产文件夹中,并通过说
加载字体
Typeface font = Typeface.createFromAsset(getAssets(), "ARDESTINE.TTF");
将此字体应用于 Button
或 TextView
等视图很容易,但是
我如何将它应用到 ActionBar
,我找不到任何 setter-method?或者我必须使用 getActionBar().setCustomView(view)
为了达到这个目的?
您可以使用所需的字体创建一个 SpannableString 并将该字符串设置为 ActionBar 的标题。
String name = "....."; // your string here
SpannableString s = new SpannableString(name);
s.setSpan(new TypefaceSpan(getApplicationContext(),BOLD_FONT_FILENAME, getResources().getColor(R.color.white), getResources().getDimension(R.dimen.size16)), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);
我还找到了上述解决方案的替代方案。将其放入您的 CreateOptionsMenu() 方法中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
this.setTitle(R.string.app_name_short);
getActionBar().setSubtitle(R.string.app_name_full);
int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
int subtitleId = getResources().getIdentifier("action_bar_subtitle", "id", "android");
Typeface font = Typeface.createFromAsset(getAssets(), "ARDESTINE.TTF");
((TextView) findViewById(titleId)).setTypeface(font);
((TextView) findViewById(subtitleId)).setTypeface(font);
// getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.checker));
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
我想自定义 ActionBar 的标题和副标题的字体。为此,我将所需的 TTF-file 放在资产文件夹中,并通过说
加载字体
Typeface font = Typeface.createFromAsset(getAssets(), "ARDESTINE.TTF");
将此字体应用于 Button
或 TextView
等视图很容易,但是
我如何将它应用到 ActionBar
,我找不到任何 setter-method?或者我必须使用 getActionBar().setCustomView(view)
为了达到这个目的?
您可以使用所需的字体创建一个 SpannableString 并将该字符串设置为 ActionBar 的标题。
String name = "....."; // your string here
SpannableString s = new SpannableString(name);
s.setSpan(new TypefaceSpan(getApplicationContext(),BOLD_FONT_FILENAME, getResources().getColor(R.color.white), getResources().getDimension(R.dimen.size16)), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);
我还找到了上述解决方案的替代方案。将其放入您的 CreateOptionsMenu() 方法中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
this.setTitle(R.string.app_name_short);
getActionBar().setSubtitle(R.string.app_name_full);
int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
int subtitleId = getResources().getIdentifier("action_bar_subtitle", "id", "android");
Typeface font = Typeface.createFromAsset(getAssets(), "ARDESTINE.TTF");
((TextView) findViewById(titleId)).setTypeface(font);
((TextView) findViewById(subtitleId)).setTypeface(font);
// getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.checker));
getMenuInflater().inflate(R.menu.main, menu);
return true;
}