您不能从 PopupWindow 视图中锚定 PopupMenu 您可以做的是从行视图或顶级视图中的视图定义或锚定这是一个示例:
主.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/IVoptionsMenuInvis"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:layout_weight="25"
android:paddingBottom="6dp"
android:paddingTop="6dp">
</FrameLayout>
弹出窗口.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/settingseditback"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/IVoptionsMenu"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:layout_weight="25"
android:onClick="showSettingsPopup2"
android:paddingBottom="6dp"
android:paddingTop="6dp"
android:src="@drawable/ic_actionbar_overflow_dark" />
</LinearLayout>
最后是你的 MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
.
.
.
public void showSettingsPopup2(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, findViewById(R.id.IVoptionsMenuInvis));
popup.setOnMenuItemClickListener(this);
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
popup.show();
}
}