PopupWindow - 没有在一些设备上工作

时间:2017-04-10 19:44:45

标签: android popupwindow android-popupwindow

我使用以下代码显示一个小弹出窗口:

public static PopupWindow showImportMenu(Activity activity, View anchor, PopupWindowClickListener onClickListener)
{
    LayoutInflater inflater = LayoutInflater.from(activity);

    PopupImportBinding binding = DataBindingUtil.inflate(inflater, R.layout.popup_import, null, false);

    if (!RootTools.isRootAvailable())
        binding.llImportRootMethod.setVisibility(View.GONE);


    PopupWindow popupWindow = new PopupWindow(activity, null, R.attr.popupMenuStyle);
    popupWindow.setFocusable(true);
    popupWindow.setContentView(binding.getRoot());
    popupWindow.setOutsideTouchable(true);
    PopupWindowCompat.showAsDropDown(popupWindow, anchor, 0, 0, Gravity.BOTTOM);

    View.OnClickListener clickListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            onClickListener.onClick(popupWindow, view);
        }
    };

    binding.llImportDefault.setOnClickListener(clickListener);
    binding.llImportRootMethod.setOnClickListener(clickListener);
    binding.llImportHTCFromContacts.setOnClickListener(clickListener);
    binding.llImportManual.setOnClickListener(clickListener);

    return popupWindow;
}

这适用于很多设备,但在某些罕见的设备上它不起作用,例如:

  • Android 5.1.1 root slim rom
  • 也许其他人......直到现在,我对其他设备了解不多

我收到的反馈是没有显示弹出窗口。有谁知道为什么这不适用于上述设备?我能做些什么才能让它在这个设备上运行?

修改

似乎不清楚我想要的是:

  • 使用showAsDropDown而非showAtLocation或类似内容,我从未在showAtLocation看到此问题
  • 我的解决方案几乎适用于所有设备,它似乎是一个特定于手机/ ROM的问题,也许它甚至无法解决,因为它可能也是设备中的一个错误=>如果有人知道这样的错误,告诉我也没关系
  • 我不想使用对话框(或其他任何东西),而不是回答我的问题。我目前使用BottomSheet对我来说很好,但我仍然想知道问题是否可以解决并以某种方式处理

7 个答案:

答案 0 :(得分:2)

就我而言,弹出窗口在少数设备上没有大小。

setContentView

之后尝试一下

50000 - 只是一个很大的尺度。

    popupWindow.getContentView().measure(50000, 50000);
    popupWindow.setWidth(popupWindow.getContentView().getMeasuredWidth());
    popupWindow.setHeight(popupWindow.getContentView().getMeasuredHeight());

您可以使用屏幕尺寸而不是50000

答案 1 :(得分:2)

在运行5.1.1的Nexus 7(不是2012)上,我遇到了同样的问题。最后通过添加以下行对其进行了修复:

popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

答案 2 :(得分:2)

我遇到了同样的问题:我的Pointer.getRenderData().setDepthTest(false); Pointer.getRenderData().setRenderingOrder(SXRRenderData.SXRRenderingOrder.OVERLAY); 在5.1.1的android设备上没有显示,但在其他设备上却显示。我意识到必须指定PopupWindowwidth才能在该版本上显示(该版本仍然与其余版本兼容)。

这里是一个例子:

height

答案 3 :(得分:1)

enter image description here

尝试QuickAction

Activity(ExampleActivity1.java),展示如何使用QuickAction:

public class Example1Activity extends Activity {
    private static final int ID_ADD = 1;
    private static final int ID_ACCEPT = 2;
    private static final int ID_UPLOAD = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.example1);

        ActionItem addItem      = new ActionItem(ID_ADD, "Add", getResources().getDrawable(R.drawable.ic_add));
        ActionItem acceptItem   = new ActionItem(ID_ACCEPT, "Accept", getResources().getDrawable(R.drawable.ic_accept));
        ActionItem uploadItem   = new ActionItem(ID_UPLOAD, "Upload", getResources().getDrawable(R.drawable.ic_up));

        //use setSticky(true) to disable QuickAction dialog being dismissed after an item is clicked
        uploadItem.setSticky(true);

        final QuickAction mQuickAction  = new QuickAction(this);

        mQuickAction.addActionItem(addItem);
        mQuickAction.addActionItem(acceptItem);
        mQuickAction.addActionItem(uploadItem);

        //setup the action item click listener
        mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
            @Override
            public void onItemClick(QuickAction quickAction, int pos, int actionId) {
                ActionItem actionItem = quickAction.getActionItem(pos);

                if (actionId == ID_ADD) {
                    Toast.makeText(getApplicationContext(), "Add item selected", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mQuickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
            @Override
            public void onDismiss() {
                Toast.makeText(getApplicationContext(), "Ups..dismissed", Toast.LENGTH_SHORT).show();
            }
        });

        Button btn1 = (Button) this.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mQuickAction.show(v);
            }
        })

        Button btn2 = (Button) this.findViewById(R.id.btn2);
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mQuickAction.show(v);
                mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
            }
        });
    }
}

输出:

enter image description here

答案 4 :(得分:0)

enter image description here ##好吧我实现了popupwindow在我的标签片段中排序,我检查工作正常一次尝试这个

我在弹出窗口的自定义布局中使用

  final PopupWindow popupWindow = new PopupWindow(getActivity());
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.popmenu1t1, null);
            l8[![enter image description here][1]][1] = (LinearLayout) view.findViewById(R.id.atoz);


  l8.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    date_sort="0";
                    discount_sort="";
                    price_sort="";
                    alpha_sort="";
                    popupWindow.dismiss();
                }
            });
            int width = 900;
            int height = 400;
            try {
                WindowManager wm = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
                width = wm.getDefaultDisplay().getWidth();
                height = wm.getDefaultDisplay().getHeight();
            } catch (Exception e) {
                e.printStackTrace();
            }
            popupWindow.setWidth(width*3/6);
            popupWindow.setFocusable(true);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setContentView(view);
            popupWindow.setBackgroundDrawable(null);
            popupWindow.setOutsideTouchable(true);
            popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

在我的应用程序中找到以下附加的屏幕截图popupwindow

答案 5 :(得分:0)

我在屏幕底部创建了一个自定义弹出对话框

public class MoreOptionDialog {

private Dialog dialog;
private Context context;
private int size;

public MoreOptionDialog(Context context) {
    this.context = context;    
}

public void showMoreOptionDialog(List<String> listMoreOption) {

    dialog = new Dialog(new ContextThemeWrapper(context, R.style.DialogSlideAnim));
    dialog.getWindow().setWindowAnimations(R.style.DialogSlideAnim);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    View view = View.inflate(context, R.layout.dialog_more_option, null);

    dialog.setContentView(view, new LinearLayout.LayoutParams(utility.getScreenWidth() - 100, size));
    dialog.getWindow().setGravity(Gravity.BOTTOM);

    ListView listView = (ListView) view.findViewById(R.id.lvMoreOption);

    MoreOptionAdapter moreOptionAdapter = new MoreOptionAdapter(context, listMoreOption, Gravity.CENTER);
    listView.setAdapter(moreOptionAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            dialog.dismiss();                
        }
    });

    dialog.show();
}
}

风格在这里

<style name="DialogSlideAnim">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
    <item name="android:windowBackground">@color/color_white</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
</style>

此对话框适用于所有设备:)

答案 6 :(得分:0)

某些ROM限制使用弹出式视图使用自己的权限。因此,用户必须明确启用权限才能显示弹出视图。

即使 MIUI 也会限制默认显示弹出式视频

请查看该ROM或设备是否有任何权限。