Android更改字体菜单项
我正在尝试将菜单项的字体更改为自定义字体。在我的操作栏中有一个下拉菜单,有几个选项。当我选择其中一个选项时,actionbar中项目的标题将更改为optiontext,并应用字体。这是因为我在覆盖的“onOptionItemSelected(MenuItem项)”中应用了te字体。我真的希望在创建菜单时应用字体(它现在使用默认字体)。因此,我尝试在“onCreateOptions菜单(菜单菜单菜单)”中执行此操作,但它始终给我一个错误(空指针异常)。我甚至尝试了多种重写方法,但似乎都不起作用。创建菜单项时,如何将自定义字体应用于该菜单项?这是我的密码:Android更改字体菜单项,android,drop-down-menu,fonts,menuitem,Android,Drop Down Menu,Fonts,Menuitem,我正在尝试将菜单项的字体更改为自定义字体。在我的操作栏中有一个下拉菜单,有几个选项。当我选择其中一个选项时,actionbar中项目的标题将更改为optiontext,并应用字体。这是因为我在覆盖的“onOptionItemSelected(MenuItem项)”中应用了te字体。我真的希望在创建菜单时应用字体(它现在使用默认字体)。因此,我尝试在“onCreateOptions菜单(菜单菜单菜单)”中执行此操作,但它始终给我一个错误(空指针异常)。我甚至尝试了多种重写方法,但似乎都不起作用。创
@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);
}
您可以自定义选项菜单,包括:
res
中创建一个名为drawable
的资源文件夹,并在其中创建边框XML或渐变XML
这一切都可以通过编程方式完成,如下所示:
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;
}
}
不要忘记在res
文件夹中创建名为menu
的文件夹,并在menu
文件夹中为菜单创建一个XML(例如cool\u menu.XML),如下所示:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="about"android:id="@+id/AboutUs" />
<item android:title="Prefs" android:id="@+id/preferences" />
<item android:title="Exit" android:id="@+id/exit" />
</menu>
然后输出将如下所示: