1、设置一个布局,比如设置一个EditText,用来显示Menu的效果。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<EditText
android:id="@+id/et_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="写这里"
/>
</LinearLayout>
2、代码实现:
public class SubMenuActivity extends Activity {
final int FONT_10 = 0x111;<span style="white-space:pre"> </span>//设置字体代码
final int FONT_12 = 0x112;
final int FONT_14 = 0x113;
final int FONT_16 = 0x114;
final int FONT_18 = 0x115;
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.submenu);
edit = (EditText) findViewById(R.id.et_edit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fontMenu = menu.addSubMenu("字体大小");<span style="white-space:pre"> </span>//创建子菜单,并添加才到呢
fontMenu.setIcon(R.drawable.ic_launcher);<span style="white-space:pre"> </span>//添加子菜单 icon
fontMenu.setHeaderIcon(R.drawable.a577710);<span style="white-space:pre"> </span>//添加子菜单的大 icon
fontMenu.setHeaderTitle("选择字体大小");<span style="white-space:pre"> </span>//添加字体标题
MenuItem font10 = fontMenu.add("10号字");<span style="white-space:pre"> </span>//添加子菜单中的一项
MenuItem font12 = fontMenu.add("12号字");
MenuItem font14 = fontMenu.add("14号字");
MenuItem font16 = fontMenu.add("16号字");
MenuItem font18 = fontMenu.add("18号字");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {<span style="white-space:pre"> </span>//菜单监听器—— onOptionsItemSelected
switch (item.getItemId()) {
case FONT_10:
edit.setTextSize(10 * 2);
break;
case FONT_12:
edit.setTextSize(12 * 2);
break;
case FONT_14:
edit.setTextSize(14 * 2);
break;
case FONT_16:
edit.setTextSize(16 * 2);
break;
case FONT_18:
edit.setTextSize(18 * 2);
break;
default:
break;
}
return true;
}
}