我打算在我的应用程序中使用快速操作UI模式.Android Quick Actions UI模式.快速操作窗口需要一个枢轴视图才能坚持下去.
quickAction.show(View pivotView);
Run Code Online (Sandbox Code Playgroud)
我打算对菜单项使用快速操作,我可以访问被点击的项目.但问题是我需要从菜单项引用一个视图,以便我可以将它传递给快速操作.
如何在所选的menuItem中引用视图.
我正在给菜单充气,并试图通过以下方式查找其中一个菜单项的视图:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// will print `null`
Log.i("TAG", String.valueOf(findViewById(R.id.action_hello)));
return true;
}
Run Code Online (Sandbox Code Playgroud)
在结果null中打印在Logcat中.但是如果我在调用之前添加一些延迟findViewById,它将返回正确的View对象:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(final Void... voids) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(final Void aVoid) {
// will print correctly android.support.v7.view.menu.ActionMenuItemView...
Log.i("TAG", String.valueOf(findViewById(R.id.action_hello)));
}
}.execute();
return true;
}
Run Code Online (Sandbox Code Playgroud)
当然,这种解决方案非常脏,最小的延迟是未知的.有没有办法为菜单膨胀事件注册一些回调.换句话说:如何 …
我需要在单击主页按钮时以编程方式打开(溢出)菜单.
正如我提供的其他主题一样Activity.openOptionsMenu(),ActionBarActivity().getSupportActionBar().openOptionsMenu()但两次都没有发生.
二手代码:
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
...
case android.R.id.home:
Log.i("HOME", "clicked");
this.openOptionsMenu();
break;
...
}
Run Code Online (Sandbox Code Playgroud)