AppCompatActivity openOptionsMenu 不工作
AppCompatActivity openOptionsMenu not working
我更新了我的问题,因为我解决了一部分,但棘手的问题仍然悬而未决:
我有一个 MainActivity extends AppCompatActivity
有碎片
我删除了 ActionBar 并放置了一个小高度的 LinearLayout,如下所示。我想要一个 ImageButton 来打开 OptionsMenu。当调用 onClick
时,为什么不调用 openOptionsMenu();
。我的意思是选项菜单没有出现。
当我点击模拟器的菜单硬件按钮时它确实出现了。
感谢您的帮助
下面,找到代码的相关部分
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="24dp"
android:clickable="false"
android:weightSum="1" >
<TextView
android:text="@string/tx_notdone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/tv_progress"
android:layout_weight="0.4" />
<ProgressBar
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="139dp"
android:layout_height="match_parent"
android:id="@+id/pb_simulation"
android:layout_weight="0.5"
android:max="100" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@android:drawable/ic_menu_manage"
android:id="@+id/btmenuImg"
android:layout_weight="0.1"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
ImageButton imgbtmenu = (ImageButton) findViewById(R.id.btmenuImg);
imgbtmenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show();
openOptionsMenu();
}
});
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
// debug
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_SHORT).show();
return true;
}
if (id == R.id.action_saveparam) {
saveparam();
return true;
}
if (id == R.id.action_loadparam) {
// debug
loadparam();
return true;
}
return super.onOptionsItemSelected(item);
}
android {
compileSdkVersion 23
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 13
targetSdkVersion 23
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
testCompile 'junit:junit:4.12'
compile project(path: ':MPChartLib')
}
我还发现了一些描述错误的内容
Issue 185217: openOptionsMenu() does not show menu when inheriting from AppCompatActivity
尝试过
@Override
public void openOptionsMenu() {
// Based loosely on http://androidxref.com/6.0.1_r10/xref/frameworks/support/v7/appcompat/src/android/support/v7/internal/app/WindowDecorActionBar.java#getDecorToolbar
final Window window = getWindow();
final View decor = window.getDecorView();
final View view = decor.findViewById(R.id.action_bar);
if (view instanceof Toolbar) {
final Toolbar toolbar = (Toolbar) view;
toolbar.showOverflowMenu();
}
}
但它不起作用。
Fabric 按钮工作正常,片段上还有另一个按钮。
该片段实现了一个 onTouch 方法(工作正常)以与其显示的图像进行交互。
谢谢
经过长时间的研究和尝试,唯一的办法就是模拟一个KeyEvent。
这使得选项菜单出现
BaseInputConnection mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
试试这个,如果你正在使用工具栏
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toolbar.showOverflowMenu();
}
}, 500);
我更新了我的问题,因为我解决了一部分,但棘手的问题仍然悬而未决:
我有一个 MainActivity extends AppCompatActivity
有碎片
我删除了 ActionBar 并放置了一个小高度的 LinearLayout,如下所示。我想要一个 ImageButton 来打开 OptionsMenu。当调用 onClick
时,为什么不调用 openOptionsMenu();
。我的意思是选项菜单没有出现。
当我点击模拟器的菜单硬件按钮时它确实出现了。
感谢您的帮助 下面,找到代码的相关部分
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="24dp"
android:clickable="false"
android:weightSum="1" >
<TextView
android:text="@string/tx_notdone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/tv_progress"
android:layout_weight="0.4" />
<ProgressBar
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="139dp"
android:layout_height="match_parent"
android:id="@+id/pb_simulation"
android:layout_weight="0.5"
android:max="100" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@android:drawable/ic_menu_manage"
android:id="@+id/btmenuImg"
android:layout_weight="0.1"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
ImageButton imgbtmenu = (ImageButton) findViewById(R.id.btmenuImg);
imgbtmenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show();
openOptionsMenu();
}
});
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
// debug
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_SHORT).show();
return true;
}
if (id == R.id.action_saveparam) {
saveparam();
return true;
}
if (id == R.id.action_loadparam) {
// debug
loadparam();
return true;
}
return super.onOptionsItemSelected(item);
}
android {
compileSdkVersion 23
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 13
targetSdkVersion 23
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
testCompile 'junit:junit:4.12'
compile project(path: ':MPChartLib')
}
我还发现了一些描述错误的内容 Issue 185217: openOptionsMenu() does not show menu when inheriting from AppCompatActivity
尝试过
@Override
public void openOptionsMenu() {
// Based loosely on http://androidxref.com/6.0.1_r10/xref/frameworks/support/v7/appcompat/src/android/support/v7/internal/app/WindowDecorActionBar.java#getDecorToolbar
final Window window = getWindow();
final View decor = window.getDecorView();
final View view = decor.findViewById(R.id.action_bar);
if (view instanceof Toolbar) {
final Toolbar toolbar = (Toolbar) view;
toolbar.showOverflowMenu();
}
}
但它不起作用。 Fabric 按钮工作正常,片段上还有另一个按钮。 该片段实现了一个 onTouch 方法(工作正常)以与其显示的图像进行交互。
谢谢
经过长时间的研究和尝试,唯一的办法就是模拟一个KeyEvent。 这使得选项菜单出现
BaseInputConnection mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
试试这个,如果你正在使用工具栏
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
toolbar.showOverflowMenu();
}
}, 500);