Android无菜单键,如何触发onCreateOptionsMenu(Menu menu)
Posted scruffybear
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android无菜单键,如何触发onCreateOptionsMenu(Menu menu)相关的知识,希望对你有一定的参考价值。
文章目录
小结
现在的android有三个键: 任务键,Home键,返回键,也就是没有菜单键了,那么如何如何触发onCreateOptionsMenu(Menu menu)这个方法呢?测试了两种办法来触发这个onCreateOptionsMenu(Menu menu)方法,以达到显示菜单的效果。
问题及解决
无法触发onCreateOptionsMenu(Menu menu)
如果没有正确设置,是不会触发onCreateOptionsMenu(Menu menu)这个方法,也就是菜单无法触发显示不出来。
修改配置文件解决
参考CSDN: 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 Overview 和 Stackoverlfow: 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
最后的效果,点以下红框中三个点的地方,可以触发菜单。
触发菜单如下红框所示:
使用一个按钮来触发
参考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();
);
点以下红框中的按钮,可以触发菜单。
点以上的按钮也是可以触发菜单的,效果跟先前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);
参考
CSDN: Android Studio 没有 gradle.properties 文件
CSDN: onCreateOptionsMenu和onPrepareOptionsMenu用法
关于在Fragment中设置Menu无效问题
CSDN: 关于 fragment重写onCreateOptionsMenu不执行问题
Stackoverflow: Handling the missing MENU button in new versions of Android (3.x and up)
Stackoverlfow: Manifest merger failed : uses-sdk:minSdkVersion 1 cannot be smaller than version 14 declared in library
Android: Progamatically Open and Close an Activity’s Option Menu
Android: App Manifest Overview
Android: Set up the app bar
Android studio: cannot find symbol variable toolbar
Stackoverflow: Error “package android.support.v7.app does not exist”
Stackoverflow: Can’t use android.support.v7.widget.Toolbar with android.useAndroidX=true
Stackoverflow: setsupportactionbar() throws error
Stackoverflow: Android error: cannot find symbol class ActionBarActivity
Stackoverflow: You need to use a Theme.AppCompat theme (or descendant) with this activity
以上是关于Android无菜单键,如何触发onCreateOptionsMenu(Menu menu)的主要内容,如果未能解决你的问题,请参考以下文章