【背景】
需要在android的actionbar中,增加一个设置setting菜单。
【折腾过程】
1.搜:
android actionbar setting menu
参考:
Android App Development How to Create an Options Menu Linux.com
但是其中提到的:
menu的xml配置和onCreateOptionsMenu,我这里都有过了。
但是显示出来的是水平位置的menu菜单,而不是三个点的那种:
2.参考:
Three dots settings menu in the Action Bar Android doesn’t appears – Stack Overflow
去再添加一个item,然后加上android:showAsAction="never"试试:
另外也参考:
Menu Resource Android Developers
看了never的含义:
never
Never place this item in the Action Bar.
然后写成:
(1)/res/menu/settings.xml
1 2 3 4 5 6 7 8 | < item android:id = "@+id/menu_settings" android:title = "@string/settings" android:showAsAction = "never" /> </ menu > |
(2)Activity中:
1 2 3 4 5 6 7 8 9 10 | @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = getMenuInflater(); //inflater.inflate(R.menu.activity_main, menu); inflater.inflate(R.menu.settings, menu); return true ; } |
但是没有显示出来。
3.参考:
How to add action bar options menu in android fragments – Stack Overflow
但是没法添加:
1 | setHasOptionsMenu( true ); |
否则会出错:
The method setHasOptionsMenu(boolean) is undefined for the type MainActivity |
因为此处是:
1 | public class MainActivity extends FragmentActivity implements ISubscriber{ |
而不是继承自fragment。
4.参考:
Three dots settings menu in the Action Bar Android doesn’t appears – Stack Overflow
说是:
android:targetSdkVersion="10" |
或更低才能出现所希望看到的,垂直方向的,三个点的配置按钮。
5.搜:
android three option menu
参考:
Show options menu button on Galaxy Nexus (Android 4.1) – Stack Overflow
解释是:
android 3.0之后,就从:
三个点的配置按钮
改为:
把配置选项都放到Actionbar中了。
所以:
此处所想要看到的,三个点的配置按钮,新版的Android已经没了。
意味着:
我此处只需要在Actionbar中加menu即可,无需,也没法,显示出那个三个点的效果了。
6.所以看来需要此处去把之前的,新加的setting菜单正常加到之前的菜单中,估计就可以正常显示了。
(1)/res/menu/activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | < item android:id = "@+id/menu_discard" android:icon = "@drawable/error_white" android:orderInCategory = "1" android:showAsAction = "ifRoom|withText" android:title = "@string/discard" /> < item android:id = "@+id/menu_send" android:icon = "@drawable/forward_white" android:orderInCategory = "2" android:showAsAction = "ifRoom|withText" android:title = "@string/send" /> < item android:id = "@+id/menu_settings" android:icon = "@drawable/settings" android:orderInCategory = "3" android:showAsAction = "ifRoom|withText" android:title = "@string/settings" /> </ menu > |
(2)代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | @Override public boolean onCreateOptionsMenu(Menu menu) { mMenu = menu; // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.activity_main, menu); //inflater.inflate(R.menu.settings, menu); //getMenuInflater().inflate(R.menu.activity_main, menu); //when first create menu, can NOT hidden them here, otherwise later will not show menus //hiddenEditMenu(); return true ; } private void hiddenEditMenu(){ if ( null != mMenu){ // for (int i = 0; i < mMenu.size(); i++){ // mMenu.getItem(i).setVisible(false); // mMenu.getItem(i).setEnabled(false); // } mMenu.findItem(R.id.menu_discard).setVisible( false ); mMenu.findItem(R.id.menu_send).setVisible( false ); } } private void showEditMenu(){ if ( null != mMenu){ // for (int i = 0; i < mMenu.size(); i++){ // mMenu.getItem(i).setVisible(true); // mMenu.getItem(i).setEnabled(true); // } mMenu.findItem(R.id.menu_discard).setVisible( true ); mMenu.findItem(R.id.menu_send).setVisible( true ); } } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.menu_discard: //Toast.makeText(MainActivity.this, "Menu Discard cliked", Toast.LENGTH_SHORT).show(); clearEditedVarValues(); return true ; case R.id.menu_send: //Toast.makeText(MainActivity.this, "Menu Send cliked", Toast.LENGTH_SHORT).show(); writeEditedVarValues(); return true ; case R.id.menu_settings: return true ; default : return super .onOptionsItemSelected(item); } } |
即可正常显示菜单,并且编辑菜单在需要时显示:
和消失:
【总结】
此处新版本Android,即3.0+,没有了三个点的那种配置选项菜单了。
所以只能把一堆的设置选项菜单都放到ActionBar上面了。
虽然很丑,但是凑合用吧。。