2012-07-19 58 views
3

我不認爲我的問題需要很多解釋,我只需要更改我的上下文菜單項的字體和大小。我怎麼做?如何更改Android ContextMenu的字體?

這裏是我的代碼:

@Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.listmenu, menu); 

    } 

這是我如何獲取Android版的默認文本菜單。但我想定製它。

+1

你是如何創建上下文菜單的工作? – FoamyGuy 2012-07-19 13:13:11

回答

2

你可以創建自己的佈局而不是R.menu.listmenu,然後設置自定義字體嗎?

或者,您可以嘗試攔截用戶的觸摸並彈出一個完全自定義的菜單,但這可能會刺激期待特定字體/菜單的用戶。

2

試試這個

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.my_menu, menu); 
getLayoutInflater().setFactory(new Factory() { 
    @Override 
    public View onCreateView(String name, Context context, AttributeSet attrs) { 
     if (name .equalsIgnoreCase(「com.android.internal.view.menu.IconMenuItemView」)) { 
      try{ 
       LayoutInflater f = getLayoutInflater(); 
       final View view = f.createView(name, null, attrs); 
       new Handler().post(new Runnable() { 
        public void run() { 
         // set the background drawable 
         view .setBackgroundResource(R.drawable.my_ac_menu_background); 

         // set the text color 
         ((TextView) view).setTextColor(Color.WHITE); 
        } 
       }); 
       return view; 
      } catch (InflateException e) { 
       } catch (ClassNotFoundException e) {} 
     } 
     return null; 
    } 
}); 
return super.onCreateOptionsMenu(menu); 
} 
+1

那麼這也適用於onCreateContextMenu? – Wise 2012-07-20 07:21:59

+0

可能是..試試吧 – 2012-07-20 09:11:21

0

測試並般的魅力:)

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_feedback_filter, menu); 

    for (int i = 0; i < menu.size(); i++) { 
     MenuItem mi = menu.getItem(i); 
     //for aapplying a font to subMenu ... 
     SubMenu subMenu = mi.getSubMenu(); 
     if (subMenu != null && subMenu.size() > 0) { 
      for (int j = 0; j < subMenu.size(); j++) { 
       MenuItem subMenuItem = subMenu.getItem(j); 
       applyFontToMenuItem(subMenuItem, typeface); 
      } 
     } 
     //the method we have create in activity 
     applyFontToMenuItem(mi, typeface); 
    } 

    return super.onCreateOptionsMenu(menu); 
} 



private void applyFontToMenuItem(MenuItem mi, Typeface font) { 
    SpannableString mNewTitle = new SpannableString(mi.getTitle()); 
    mNewTitle.setSpan(new CustomTypefaceSpan("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    mi.setTitle(mNewTitle); 
} 

定製跨度類

import android.graphics.Paint; 
import android.graphics.Typeface; 
import android.text.TextPaint; 
import android.text.style.TypefaceSpan; 

public class CustomTypefaceSpan extends TypefaceSpan { 

    private final Typeface newType; 

    public CustomTypefaceSpan(String family, Typeface type) { 
     super(family); 
     newType = type; 
    } 

    @Override 
    public void updateDrawState(TextPaint ds) { 
     applyCustomTypeFace(ds, newType); 
    } 

    @Override 
    public void updateMeasureState(TextPaint paint) { 
     applyCustomTypeFace(paint, newType); 
    } 

    private static void applyCustomTypeFace(Paint paint, Typeface tf) { 
     int oldStyle; 
     Typeface old = paint.getTypeface(); 
     if (old == null) { 
      oldStyle = 0; 
     } else { 
      oldStyle = old.getStyle(); 
     } 

     int fake = oldStyle & ~tf.getStyle(); 
     if ((fake & Typeface.BOLD) != 0) { 
      paint.setFakeBoldText(true); 
     } 

     if ((fake & Typeface.ITALIC) != 0) { 
      paint.setTextSkewX(-0.25f); 
     } 

     paint.setTypeface(tf); 
    } 
}