最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】android中actionbar中增加setting菜单

Android crifan 5211浏览 0评论

【背景】

需要在android的actionbar中,增加一个设置setting菜单。

【折腾过程】

1.搜:

android actionbar setting menu

参考:

Android App Development How to Create an Options Menu  Linux.com

Menus  Android Developers

但是其中提到的:

menu的xml配置和onCreateOptionsMenu,我这里都有过了。

但是显示出来的是水平位置的menu菜单,而不是三个点的那种:

android three dot 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

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:title="@string/settings"
        android:showAsAction="never" />

</menu>

(2)Activity中:

	@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

但是没法添加:

setHasOptionsMenu(true);

否则会出错:

The method setHasOptionsMenu(boolean) is undefined for the type MainActivity

因为此处是:

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

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <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)代码:

	@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 menu settings show edit menus

和消失:

android show menu settings hidden edit menus

 

【总结】

此处新版本Android,即3.0+,没有了三个点的那种配置选项菜单了。

所以只能把一堆的设置选项菜单都放到ActionBar上面了。

虽然很丑,但是凑合用吧。。

转载请注明:在路上 » 【已解决】android中actionbar中增加setting菜单

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.176 seconds, using 22.13MB memory