有谁知道我如何使用 ImageButton
并模仿相应菜单的弹出窗口?也就是说,让菜单显示在按钮下方。我应该使用上下文菜单吗?
基本上,除了我没有使用 ActionBar 之外,我正在尝试使菜单显示如下。
最佳答案
我想你在谈论 PopupMenu .如果是这样,可以通过以下方式完成:
public class MyActivity extends Activity {
private PopupMenu mPopupMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
mPopupMenu = new PopupMenu(this, imageButton);
MenuInflater menuInflater = mPopupMenu.getMenuInflater();
menuInflater.inflate(R.menu.mymenu, mPopupMenu.getMenu());
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopupMenu.show();
}
});
}
}
结果如下:
关于android - 用 ImageButton 模仿 ActionBar 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22838305/