Android无菜单键,如何触发onCreateOptionsMenu(Menu menu)

奋斗吧
奋斗吧
擅长邻域:未填写

标签: Android无菜单键,如何触发onCreateOptionsMenu(Menu menu) 办公效率博客 51CTO博客

2023-07-20 18:24:17 196浏览

Android无菜单键,如何触发onCreateOptionsMenu(Menu menu),回键,也就是没有菜单键了,那么如何如何触发onCreateOptionsMenu(Menumenu)这个方法呢?测试了两种办法来触发这个onCreateOptionsMenu(Menumenu)方法,以达到显示菜单的效果。


文章目录

  • 小结
  • 问题及解决
  • 无法触发onCreateOptionsMenu(Menu menu)
  • 修改配置文件解决
  • 使用一个按钮来触发
  • 其它办法
  • 参考

小结

现在的Android有三个键: 任务键,Home键,返回键,也就是没有菜单键了,那么如何如何触发onCreateOptionsMenu(Menu menu)这个方法呢?测试了两种办法来触发这个onCreateOptionsMenu(Menu menu)方法,以达到显示菜单的效果。

问题及解决

无法触发onCreateOptionsMenu(Menu menu)

如果没有正确设置,是不会触发onCreateOptionsMenu(Menu menu)这个方法,也就是菜单无法触发显示不出来。

修改配置文件解决

参 Android Studio 没有 gradle.properties 文件,需要在工程根目录下创建gradle.properties这个文件,内容如下:

android.useAndroidX=true
android.enableJetifier=true

参考关于在Fragment中设置Menu无效问题,在AndroidManifest.xml里添加android:theme="@style/ThemeOverlay.AppCompat.ActionBar"这行,以激活ActionBar的使用。
AndroidManifest.xml的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	  package="example.menu"
	  android:versionCode="1"
	  android:versionName="1.0"
	 android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".MenuTest"
				  android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
</manifest>

dependencies是这样设置的:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    //implementation 'androidx.appcompat:appcompat:1.0.0'
    //implementation 'android.support.v7.widget'
    //compile 'com.android.support:appcompat-v7:21.0.+'
    implementation 'com.android.support:appcompat-v7:29.0.+'
}

碰到以下问题:

uses-sdk:minSdkVersion 1 cannot be smaller than version 14 declared in library [androidx.appcompat:appcompat:1.0.0] C:\Users\LENOVO\.gradle\caches\transforms-3\9bee862a3eadd8338cd2918c9dad0196\transformed\appcompat-1.0.0\AndroidManifest.xml as the library might be using APIs not available in 1 Suggestion: use a compatible library with a minSdk of at most 1, or increase this project's minSdk version to at least 14, or use tools:overrideLibrary="androidx.appcompat" to force usage (may lead to runtime failures) 

参考Android: App Manifest OverviewStackoverlfow: Manifest merger failed : uses-sdk:minSdkVersion 1 cannot be smaller than version 14 declared in library

build.gradle里添加

defaultConfig {
// other config options...
        minSdkVersion 14
    }

添加了以下的包引用:

import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;

参考Stackoverflow: You need to use a Theme.AppCompat theme (or descendant) with this activity,注意:
MenuTest 是继承自Activity,而不是AppCompatActivity或者ActionBarActivity .

public class MenuTest extends Activity
//public class MenuTest extends AppCompatActivity

最后的效果,点以下红框中三个点的地方,可以触发菜单。

Android无菜单键,如何触发onCreateOptionsMenu(Menu menu)_xml


触发菜单如下红框所示:

Android无菜单键,如何触发onCreateOptionsMenu(Menu menu)_android_02

使用一个按钮来触发

参考Stackoverflow: Handling the missing MENU button in new versions of Android (3.x and up)
Android: Progamatically Open and Close an Activity’s Option Menu

在界面中添加一个按钮Pop Up Menu,可以在按钮事件中添加 openOptionsMenu();这个方法来触发菜单的。在``方法中添加以下监听器:

Button button = (Button) findViewById(R.id.button_send);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Do something in response to button click
				openOptionsMenu();
			}
		});

点以下红框中的按钮,可以触发菜单。

Android无菜单键,如何触发onCreateOptionsMenu(Menu menu)_gradle_03


点以上的按钮也是可以触发菜单的,效果跟先前Actionbar触发菜单是一样的。

最后,整个的build.gradle文件内容如下:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.0'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    //implementation 'androidx.appcompat:appcompat:1.0.0'
    //implementation 'android.support.v7.widget'
    //compile 'com.android.support:appcompat-v7:21.0.+'
    implementation 'com.android.support:appcompat-v7:29.0.+'

}

allprojects {
    repositories {
        google()
        //jcenter()
    }

}



android {
    compileSdk 29
    buildToolsVersion '29.0.0'

    defaultConfig {
// other config options...
        minSdkVersion 14
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        //instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

整个的源代码如下:

package example.menu;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.EditText;
import android.widget.Toast;

import android.app.Fragment;
//import androidx.fragment.app.Fragment;
//import android.support.v7.widget.Toolbar;
//import androidx.appcompat.app.AppCompatActivity;
//import androidx.appcompat.app.AppCompatActivity;
//import androidx.appcompat.widget.Toolbar;

import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
//import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.AppCompatActivity;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class MenuTest extends Activity
//public class MenuTest extends AppCompatActivity
{

	final int FONT_10 = 0x111;
	final int FONT_12 = 0x112;
	final int FONT_14 = 0x113;
	final int FONT_16 = 0x114;
	final int FONT_18 = 0x115;

	final int PLAIN_ITEM = 0x11b;

	final int FONT_RED = 0x116;
	final int FONT_BLUE = 0x117;
	final int FONT_GREEN = 0x118;
	
	private EditText edit;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		edit = (EditText) findViewById(R.id.txt);

//		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//		setSupportActionBar(toolbar);
		//setHasOptionsMenu(true);
//		Toolbar toolbar = findViewById(R.id.toolbar);
//		toolbar.inflateMenu(R.menu.menu_main);
		//setHasOptionsMenu(true);//call it from onCreate(); or onViewCreated();
		//Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
		//android.widget.Toolbar myToolbar = (android.widget.Toolbar) findViewById(R.id.toolbar);
		//setSupportActionBar(myToolbar);
		//setSupportActionBar(myToolbar);
		//((MainActivity) getActivity()).setSupportActionBar(myToolbar);

		Button button = (Button) findViewById(R.id.button_send);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Do something in response to button click
				openOptionsMenu();
			}
		});
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{

		SubMenu fontMenu = menu.addSubMenu("Font Size");

		fontMenu.setIcon(R.drawable.font);

		fontMenu.setHeaderIcon(R.drawable.font);

		fontMenu.setHeaderTitle("Select font size");
		fontMenu.add(0, FONT_10, 0, "10 font size");
		fontMenu.add(0, FONT_12, 0, "12 font size");
		fontMenu.add(0, FONT_14, 0, "14 font size");
		fontMenu.add(0, FONT_16, 0, "16 font size");
		fontMenu.add(0, FONT_18, 0, "18 font size");

		menu.add(0, PLAIN_ITEM, 0, "Normal Menu Selection (Toast)");

		SubMenu colorMenu = menu.addSubMenu("Text Color");
		colorMenu.setIcon(R.drawable.color);

		colorMenu.setHeaderIcon(R.drawable.color);

		colorMenu.setHeaderTitle("Select Text Color");
		colorMenu.add(0, FONT_RED, 0, "Red");
		colorMenu.add(0, FONT_GREEN, 0, "Green");
		colorMenu.add(0, FONT_BLUE, 0, "Blue");
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.job_status_option_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}


	@Override

	public boolean onOptionsItemSelected(MenuItem mi)
	{

		switch (mi.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;
			case FONT_RED:
				edit.setTextColor(Color.RED);
				break;
			case FONT_GREEN:
				edit.setTextColor(Color.GREEN);
				break;
			case FONT_BLUE:
				edit.setTextColor(Color.BLUE);
				break;
			case PLAIN_ITEM:
				Toast toast = Toast.makeText(MenuTest.this
					, "This is a toast message!" , Toast.LENGTH_SHORT);
				toast.show();
				break;
		}
		return true;
	}


}

main.xml的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<EditText 
	android:id="@+id/txt" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:text="For Test Purpose! "
	android:editable="false"
	/>

	<Button xmlns:android="http://schemas.android.com/apk/res/android"
		android:id="@+id/button_send"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Pop Up Menu"
		android:onClick="popUpMenu" />
</LinearLayout>

其它办法

参考Android: Set up the app bar 可以使用setSupportActionBar(myToolbar);Toolbar来触发onCreateOptionsMenu(Menu menu)这个方法,这个没试过,不清楚效果怎么样。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
}


好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695