如何在 Android 中创建自定义 PopupMenu
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23400732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a custom PopupMenu in Android
提问by lschlessinger
How can I replicate something like I made below in Balsamiq?
如何复制我在 Balsamiq 中制作的类似内容?
I made this menu, but it is only displaying the text of the items (not the icons). Is it possible to display both the title and icon in a PopupMenu?
我制作了这个菜单,但它只显示项目的文本(而不是图标)。是否可以在 PopupMenu 中同时显示标题和图标?
Here is my create_post_menu.xml
这是我的 create_post_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_photo"
android:icon="@drawable/ic_action_camera"
android:title="@string/action_photo"
android:showAsAction="always|withText" />
<item
android:id="@+id/action_video"
android:icon="@drawable/ic_action_video"
android:title="@string/action_video"
android:showAsAction="always|withText" />
<item
android:id="@+id/action_text"
android:icon="@drawable/ic_action_edit"
android:title="@string/action_text"
android:showAsAction="always|withText" />
<item
android:id="@+id/action_link"
android:icon="@drawable/ic_action_web_site"
android:title="@string/action_link"
android:showAsAction="always|withText" />
</menu>
Edit
编辑
Here are my onCreateOptionsMenu
and onOptionsItemSelected
methods:
以下是我的方法onCreateOptionsMenu
和onOptionsItemSelected
方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_new) {
View menuItemView = findViewById(R.id.action_new);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.create_post_menu);
popupMenu.show();
return true;
} else if(item.getItemId() == R.id.action_search) {
return true;
} else if(item.getItemId() == R.id.action_settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
} else if(item.getItemId() == R.id.action_help) {
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
回答by lschlessinger
I resolved this issue by simply putting the create_post_menu
inside of the item
whose icon is a +
.
我简单地把解决这个问题create_post_menu
的内部item
,其图标是一个+
。
For example, I have (using AppCompat):
例如,我有(使用 AppCompat):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_new"
android:icon="@drawable/ic_action_new"
android:title="@string/action_new"
app:showAsAction="always">
<menu>
<item
android:id="@+id/action_photo"
android:icon="@drawable/ic_action_camera"
android:title="@string/action_photo"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_video"
android:icon="@drawable/ic_action_video"
android:title="@string/action_video"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_text"
android:icon="@drawable/ic_action_text"
android:title="@string/action_text"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_place"
android:icon="@drawable/ic_action_place"
android:title="@string/action_place"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_more"
android:title="@string/action_more"
android:visible="false"
app:showAsAction="always|withText" />
</menu>
</item>
...(more menu items here)
</menu>
Without AppCompat, you could just get rid of the XML Namespace app
by replacing app
with android
.
如果没有 AppCompat,您可以app
通过替换app
为.xml 来摆脱 XML 命名空间android
。
回答by user3585662
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);//your created butto
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
}