可以为 PopupWindow 设置焦点(假)
按钮仍然可以点击,但没有视觉点击行为(一些自定义处理程序强制显示点击?)
下面是带有“始终在顶部”选项的浮动窗口的示例
浮动窗口附近的原始布局在这两种情况下都是完全可操作的,此外,当窗口仍处于浮动状态时,可以使用对话框和其他弹出窗口
窗户也是可重复使用的
final static int buttonAlpha = 0xDF;
final static float buttonTextSize = 12f;
public final void addPopupButton(LinearLayout linearLayout, String title, android.view.View.OnClickListener onClickListener)
{
Button button = new Button(this.getContext());
button.setText(title);
button.setTextSize(buttonTextSize);
button.getBackground().setAlpha(buttonAlpha);
button.setOnClickListener(onClickListener);
linearLayout.addView(button, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
public final Button addPopupCheckbox(LinearLayout linearLayout, String title, boolean isChecked, android.view.View.OnClickListener onClickListener)
{
final Button button = new Button(getContext());
button.setText(title);
button.setTextSize(buttonTextSize);
final int buttonHeight = button.getHeight();
setButtonChecked(button, isChecked);
button.setHeight(buttonHeight);
button.getBackground().setAlpha(buttonAlpha);
button.setOnClickListener(onClickListener);
linearLayout.addView(button, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
return button;
}
public final void setButtonChecked(Button button, boolean isChecked)
{
button.setCompoundDrawablesWithIntrinsicBounds(Resources.getSystem().getIdentifier(isChecked ? "android:drawable/btn_check_on" : "android:drawable/btn_check_off", null, null), 0, 0, 0);
}
private boolean isMenuAlwaysOnTop = true;
private PopupWindow popupWindowMenuV2 = null;
public final void popupMenuNav2()
{
if (popupWindowMenuV2 == null)
{
// [start] layout
ScrollView scrollView = new ScrollView(this.getContext());
final LinearLayout linearLayoutNavigation = new LinearLayout(this.getContext());
linearLayoutNavigation.setOrientation(LinearLayout.VERTICAL);
linearLayoutNavigation.setBackgroundColor(0x7FFFFFFF);
linearLayoutNavigation.setPadding(20, 10, 20, 10);
scrollView.addView(linearLayoutNavigation, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
popupWindowMenuV2 = new PopupWindow(this);
popupWindowMenuV2.setBackgroundDrawable(new BitmapDrawable());
popupWindowMenuV2.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindowMenuV2.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindowMenuV2.setTouchable(true);
popupWindowMenuV2.setOutsideTouchable(!isMenuAlwaysOnTop);
popupWindowMenuV2.setFocusable(!isMenuAlwaysOnTop);
popupWindowMenuV2.setTouchInterceptor(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_OUTSIDE)
{
if (!isMenuAlwaysOnTop)
popupWindowMenuV2.dismiss();
else
return false;
return true;
}
return false;
}
});
popupWindowMenuV2.setContentView(scrollView);
// [end] layout
// [start] always on top checkbox
final Button buttonMenuAlwaysOnTop = addPopupCheckbox(linearLayoutNavigation, "always on top", isMenuAlwaysOnTop, null);
buttonMenuAlwaysOnTop.setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View vv)
{
isMenuAlwaysOnTop = !isMenuAlwaysOnTop;
setButtonChecked(buttonMenuAlwaysOnTop, isMenuAlwaysOnTop);
popupWindowMenuV2.dismiss();
popupWindowMenuV2.setOutsideTouchable(!isMenuAlwaysOnTop);
popupWindowMenuV2.setFocusable(!isMenuAlwaysOnTop);
popupWindowMenuV2.showAtLocation(((Activity) getContext()).getWindow().getDecorView(), Gravity.CENTER_VERTICAL + Gravity.RIGHT, 0, 0);
}
});
// [end] always on top checkbox
addPopupButton(linearLayoutNavigation, "some button",
new OnClickListener()
{
@Override
public void onClick(View vv)
{
if (!isMenuAlwaysOnTop)
popupWindowMenuV2.dismiss();
someAction();
}
});
}
popupWindowMenuV2.showAtLocation(((Activity) getContext()).getWindow().getDecorView(), Gravity.CENTER_VERTICAL + Gravity.RIGHT, 0, 0);
}
// somewhere in handler:
if (someCondition)
{
if (popupWindowMenuV2 != null && popupWindowMenuV2.isShowing())
popupWindowMenuV2.dismiss();
else
popupMenuNav2();
return true;
}