当我使用默认的sherlock light主题时,我可以通过此方法更改字体(可以将其强制转换为TextView)
@Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main,menu); getLayoutInflater().setFactory(new LayoutInflater.Factory() { public View onCreateView(String name,Context context,AttributeSet attrs) { if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView") || name.equalsIgnoreCase("TextView")) { try { LayoutInflater li = LayoutInflater.from(context); final View view = li.createView(name,null,attrs); new Handler().post(new Runnable() { public void run() { // here I can change the font! ((TextView)view).setTypeface(MY_CUSTOM_TYPE_FACE); } }); return view; } catch (InflateException e) { // Handle any inflation exception here } catch (ClassNotFoundException e) { // Handle any ClassNotFoundException here } } return null; } }); return true; }
但是当我使用this tool的自定义主题时,上述解决方案不起作用.这样每个项目都是ActionMenuItemView的一个实例,我不知道如何将字体应用于它.
解决方法
需要创建自定义菜单视图
private ActionBar setCustomView(ActionBar actionBar,String title,String subtitle,boolean isSubTitleEnable){ LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(R.layout.customer_view,null); TextView tv = (TextView) v.findViewById(R.id.cust_action_bar_title); Typeface tf = Typeface.createFromAsset(this.getAssets(),"YOURFONT.ttf"); tv.setTypeface(tf); tv.setText(title); actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayOptions(0,actionBar.DISPLAY_SHOW_TITLE); actionBar.setDisplayShowCustomEnabled(true); actionBar.setCustomView(v); return actionBar; }