我试图将菜单项的字体更改为自定义字体。在我的操作栏中有一个带有几个选项的下拉菜单。当我选择一个这些选项时,操作栏中项目的标题将更改为此选项文本,它将应用该字体。这是因为我在覆盖的'onOptionItemSelected(MenuItem item)'中应用了te字体。我真的希望在创建菜单时应用字体(它现在使用默认字体)。所以我试图在' onCreateOptionsMenu(菜单菜单)'中执行此操作,但它一直给我一个错误(空指针异常)。我甚至尝试了多种重写方法,但似乎没有任何效果。如何在创建菜单项时将自定义字体应用于菜单项?这是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu){
Log.d("GraphActivity", "onCreateOptionsMenu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_graph, menu);
optionMenu = menu;
Boolean dayMatch = User.getInstance().getDownloadedDay() == User.getInstance().getCurrentDay();
Boolean monthMatch = User.getInstance().getDownloadedMonth() == User.getInstance().getCurrentMonth();
Boolean yearMatch = User.getInstance().getDownloadedYear() == User.getInstance().getCurrentYear();
if(! dayMatch || ! monthMatch || ! yearMatch){
FeedbackManager feedbackManager = new FeedbackManager();
feedbackManager.showToast("file is out of date", GraphActivity.this, Toast.LENGTH_SHORT);
optionMenu.findItem(R.id.action_refresh).setIcon(R.drawable.ic_action_refreshed);
}
return true;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu){
Typeface typeface = Typeface.createFromAsset(getAssets(), "TitilliumWeb-Regular.ttf");
menuItemView = (TextView)findViewById(R.id.action_select);
menuItemView.setTypeface(typeface);
return super.onMenuOpened(featureId, menu);
}
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu)
{
Typeface typeface = Typeface.createFromAsset(getAssets(), "TitilliumWeb-Regular.ttf");
menuItemView = (TextView)findViewById(R.id.action_select);
menuItemView.setTypeface(typeface);
return super.onCreatePanelMenu(featureId, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
Log.d("GraphActivity", "onOptionsItemSelected");
Typeface typeface = Typeface.createFromAsset(getAssets(), "TitilliumWeb-Regular.ttf");
menuItemView = (TextView)findViewById(R.id.action_select);
menuItemView.setTypeface(typeface);
int id = item.getItemId();
if(id == R.id.action_settings){
Intent intent = new Intent(GraphActivity.this, SettingsActivity.class);
startActivity(intent);
}else if(id == R.id.action_refresh){
if(itemClickable){
TaskManager taskManager = new TaskManager(GraphActivity.this, progressBar);
taskManager.runTask("online");
}
}else if(id == R.id.action_current){
graphManager.setSelect(GraphManager.select_current);
String title = optionMenu.findItem(R.id.action_current).getTitle().toString();
optionMenu.findItem(R.id.action_select).setTitle(title);
setSelectable(WEEK_button);
setSelectable(MONTH_button);
setClickable(YEAR_button);
graph();
}else if(id == R.id.action_previous){
graphManager.setSelect(GraphManager.select_previous);
String title = optionMenu.findItem(R.id.action_previous).getTitle().toString();
optionMenu.findItem(R.id.action_select).setTitle(title);
setSelectable(WEEK_button);
setSelectable(MONTH_button);
setClickable(YEAR_button);
graph();
}else if(id == R.id.action_compare){
graphManager.setSelect(GraphManager.select_compare);
graphManager.setPeriod(GraphManager.period_year);
String title = optionMenu.findItem(R.id.action_compare).getTitle().toString();
optionMenu.findItem(R.id.action_select).setTitle(title);
setNonSelectable(WEEK_button);
setNonSelectable(MONTH_button);
setHighLight(YEAR_button);
setNotClickable(YEAR_button);
graph();
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:0)
您可以自定义选项菜单,包括:
添加自定义字体
更改字体大小
更改字体颜色
将背景设置为可绘制资源(例如图像,边框,渐变)
要将背景更改为边框或渐变,您必须在名为res
的{{1}}中创建资源文件夹,并在其中创建边框XML或渐变XML。
这可以通过编程方式完成,如下所示:
drawable
不要忘记在public class CustomMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cool_menu, menu);
getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name.equalsIgnoreCase(
"com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
// set the background drawable if you want that
//or keep it default -- either an image, border
//gradient, drawable, etc.
view.setBackgroundResource(R.drawable.myimage);
((TextView) view).setTextSize(20);
// set the text color
Typeface face = Typeface.createFromAsset(
getAssets(),"OldeEnglish.ttf");
((TextView) view).setTypeface(face);
((TextView) view).setTextColor(Color.RED);
}
});
return view;
} catch (InflateException e) {
//Handle any inflation exception here
} catch (ClassNotFoundException e) {
//Handle any ClassNotFoundException here
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.AboutUs:
Intent i = new Intent("com.test.demo.ABOUT");
startActivity(i);
break;
case R.id.preferences:
Intent p = new Intent("com.test.demo.PREFS");
startActivity(p);
break;
case R.id.exit:
finish();
break;
}
return false;
}
}
文件夹中创建名为menu
的文件夹,并在res
文件夹中为您的菜单创建一个XML(例如 cool_menu.xml )比如:
menu
然后输出将是这样的: