菜单定义:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/font_size" android:showAsAction="always|withText" android:icon="@drawable/font"> <menu> <!-- 定义一组单选菜单项 --> <group android:checkableBehavior="single"> <!-- 定义多个菜单项 --> <item android:id="@+id/font_10" android:title="@string/font_10"/> <item android:id="@+id/font_12" android:title="@string/font_12"/> <item android:id="@+id/font_14" android:title="@string/font_14"/> <item android:id="@+id/font_16" android:title="@string/font_16"/> <item android:id="@+id/font_18" android:title="@string/font_18"/> </group> </menu> </item> <!-- 定义一个普通菜单项 --> <item android:id="@+id/plain_item" android:showAsAction="always|withText" android:title="@string/plain_item"> </item> <item android:title="@string/font_color" android:showAsAction="always" android:icon="@drawable/color"> <menu> <!-- 定义一组允许复选的菜单项 --> <group> <!-- 定义三个菜单项 --> <item android:id="@+id/red_font" android:title="@string/red_title"/> <item android:id="@+id/green_font" android:title="@string/green_title"/> <item android:id="@+id/blue_font" android:title="@string/blue_title"/> </group> </menu> </item> </menu>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 定义一组单选菜单项目 --> <group android:checkableBehavior="single"> <!-- 定义三个菜单项 --> <item android:id="@+id/red" android:title="@string/red_title" android:alphabeticShortcut="r"/> <item android:id="@+id/green" android:title="@string/green_title" android:alphabeticShortcut="g"/> <item android:id="@+id/blue" android:title="@string/blue_title" android:alphabeticShortcut="b"/> </group> </menu>
界面定义:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="15pt" android:text="可通过上下文菜单修改背景色" /> </LinearLayout>
字符串资源:
<resources> <string name="app_name">ActionItemTest</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="red_title">红色</string> <string name="blue_title">蓝色</string> <string name="green_title">绿色</string> <string name="font_size">字体大小</string> <string name="font_color">字体颜色</string> <string name="plain_item">普通菜单项</string> <string name="font_10">10号字体</string> <string name="font_12">12号字体</string> <string name="font_14">14号字体</string> <string name="font_16">16号字体</string> <string name="font_18">18号字体</string> </resources>
代码:
package allegro.actionbartes; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt = (TextView) findViewById(R.id.txt); // 为文本框注册上下文菜单 registerForContextMenu(txt); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflator = new MenuInflater(this); //装填R.menu.my_menu对应的菜单,并添加到menu中 inflator.inflate(R.menu.menu_main, menu); return super.onCreateOptionsMenu(menu); } // 创建上下文菜单时触发该方法 @Override public void onCreateContextMenu(ContextMenu menu, View source, ContextMenu.ContextMenuInfo menuInfo) { MenuInflater inflator = new MenuInflater(this); //装填R.menu.context对应的菜单,并添加到menu中 inflator.inflate(R.menu.context , menu); menu.setHeaderIcon(R.drawable.tools); menu.setHeaderTitle("请选择背景色"); } // 上下文菜单中菜单项被单击时触发该方法 @Override public boolean onContextItemSelected(MenuItem mi) { // 勾选该菜单项 mi.setChecked(true); // ① switch (mi.getItemId()) { case R.id.red: mi.setChecked(true); txt.setBackgroundColor(Color.RED); break; case R.id.green: mi.setChecked(true); txt.setBackgroundColor(Color.GREEN); break; case R.id.blue: mi.setChecked(true); txt.setBackgroundColor(Color.BLUE); break; } return true; } @Override // 菜单项被单击后的回调方法 public boolean onOptionsItemSelected(MenuItem mi) { if(mi.isCheckable()) { // 勾选该菜单项 mi.setChecked(true); // ② } //判断单击的是哪个菜单项,并有针对性地作出响应 switch (mi.getItemId()) { case R.id.font_10: txt.setTextSize(10 * 2); break; case R.id.font_12: txt.setTextSize(12 * 2); break; case R.id.font_14: txt.setTextSize(14 * 2); break; case R.id.font_16: txt.setTextSize(16 * 2); break; case R.id.font_18: txt.setTextSize(18 * 2); break; case R.id.red_font: txt.setTextColor(Color.RED); mi.setChecked(true); break; case R.id.green_font: txt.setTextColor(Color.GREEN); mi.setChecked(true); break; case R.id.blue_font: txt.setTextColor(Color.BLUE); mi.setChecked(true); break; case R.id.plain_item: Toast toast = Toast.makeText(MainActivity.this , "您单击了普通菜单项" , Toast.LENGTH_SHORT); toast.show(); break; } return true; } }