问题描述
在Android的API11 +我显示PopupWindow里面的按钮。我想表现出的PopupMenu单击该按钮时,无需关闭PopupWindow。这是可能的呢?
我实例化和initilizing的弹出菜单,但是当我打电话popupMenu.show()
我得到这个错误(LogCat中+部分调用堆栈):
In Android API11+ I'm displaying a button inside a PopupWindow. I'd like to show a PopupMenu when the button is clicked, without closing the PopupWindow. Is this possible at all?I'm instantiating and initilizing the PopupMenu, but when I call popupMenu.show()I'm getting this error (LogCat + partial call stack):
13 02-25:31:38.281:W /窗口管理器(528):试图用令牌是一个子窗口中添加窗口:android.os.BinderProxy@41316cc8。中止。
02-25 13:31:51.257:D / AndroidRuntime(7643):关闭VM
02-25 13:31:51.257:W / dalvikvm(7643):主题ID = 1:螺纹未捕获的异常(组= 0x40a711f8)退出
02-25 13:31:51.320:E / AndroidRuntime(7643):致命异常:主要
02-25 13:31:51.320:E / AndroidRuntime(7643):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl$W@419c3458无效;是您的活动运行?
[堆栈其余]
02-25 13:31:38.281: W/WindowManager(528): Attempted to add window with token that is a sub-window: android.os.BinderProxy@41316cc8. Aborting.02-25 13:31:51.257: D/AndroidRuntime(7643): Shutting down VM02-25 13:31:51.257: W/dalvikvm(7643): threadid=1: thread exiting with uncaught exception (group=0x40a711f8)02-25 13:31:51.320: E/AndroidRuntime(7643): FATAL EXCEPTION: main02-25 13:31:51.320: E/AndroidRuntime(7643): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@419c3458 is not valid; is your activity running?[rest of stack]
这是因为PopupMenu的使用另一个PopupWindow这似乎并没有被德从PopupWindow允许?同样的code运行正常,当我把它挂在活动内容视图按钮。
Is this because the PopupMenu uses another PopupWindow which does not seem te be allowed from a PopupWindow? Same code runs OK when I hook it up to a button in the Activity content view.
由于提前,
推荐答案
您不能从PopupWindow视图,你可以做的是从视图中roow此处查看或顶层的观点是一个定义或锚锚定的PopupMenu例如:
You can't anchor a PopupMenu from a PopupWindow view what you can do is define or anchor from a view inside roow view or top-level view here is an example:
main.xml中:
main.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>
popupwindow.xml:
popupwindow.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:
and finally your 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();
}
}
这篇关于在PopupWindow视图可以显示一个PopupMenu的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!