如何在android中更改菜单项“字体样式”

如何在android中更改菜单项“字体样式”,android,Android,我想更改菜单项的“字体样式”。 默认情况下是这样的 但我想换成这个 您可以自定义选项菜单,包括: 1.添加自定义字体 2.更改字体大小 3.更改字体颜色 4.将背景设置为可绘制资源(例如图像、边框、渐变) 要将背景更改为边框或渐变,必须在res中创建一个名为drawable的资源文件夹,并在其中创建边框XML或渐变XML 这一切都可以通过编程方式完成,如下所示: public class CustomMenu extends Activity { /** Called when the act

我想更改菜单项的“字体样式”。 默认情况下是这样的

但我想换成这个

您可以自定义选项菜单,包括:

1.添加自定义字体

2.更改字体大小

3.更改字体颜色

4.将背景设置为可绘制资源(例如图像、边框、渐变)

要将背景更改为边框或渐变,必须在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_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>

您可以自定义选项菜单,包括:

1.添加自定义字体

2.更改字体大小

3.更改字体颜色

4.将背景设置为可绘制资源(例如图像、边框、渐变)

要将背景更改为边框或渐变,必须在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_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>