如果您正在寻找教程,这里有一些您可以使用的资源:
https://developer.android.com/guide/topics/ui/menus.html#context-menu
http://wptrafficanalyzer.in/blog/creating-a-contextual-menu-bar-contextual-action-mode-for-a-single-view-in-android/
http://mobile.tutsplus.com/tutorials/android/android-sdk-context-menus/
基本上,您可以创建一个布局并在单击按钮时对其进行膨胀:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="@+id/menuItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/menu1" />
<TextView
android:id="@+id/menuItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/menu2" />
<TextView
android:id="@+id/menuItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/menu3" />
</LinearLayout>
在您的 showPopup() 方法中,您可以执行以下操作:
public void showPopup(View v) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(
R.layout.container, null, false), 400, 500, true);
pw.showAtLocation(findViewById(R.id.menu_layout), Gravity.CENTER, 0,
0);
}