2012-10-06 25 views
7

私はこの方法でフォントフェースを変更することができ、デフォルトのシャーロック光のテーマを使用しています(それがTextViewのにキャストすることができます)アクションバーのカスタムテーマを使用しているときに、actionbarsherlockメニュー項目のフォントフェースを変更する方法は?

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

actiobarsherlock with default light theme

が、私はthis toolでカスタムテーマを使用して、上記解決策は機能しません。この方法では、各アイテムはActionMenuItemViewのインスタンスであり、フォントフェースをどのように適用するのかわかりません。

actiobarsherlock with custom theme

+1

を作成する必要がありますか?私にも同様の問題があります。 – Philio

答えて

0

あなたはこの解決策を見つけるか、カスタムメニュービュー

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; 
} 
関連する問題