PopupWindow - not working on a few devices

扶醉桌前 提交于 2020-01-23 04:26:12

问题


I use following code to show a small popup:

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;
}

This works on a lot of devices but on some rare devices it does not work, like:

  • Android 5.1.1 root slim rom
  • maybe others... until now, I don't know more about other devices

I got the feedback that no popup is shown. Does anyone know why this is not working on the above mentioned device? And what I can do to make it work on this device as well?

EDIT

It seems like it's not clear that what I want is following:

  • use showAsDropDown not showAtLocation or similar, I never saw this problem with showAtLocation yet
  • my solution is working on nearly all devices, it seems to be a phone/rom specific problem, maybe it's not even solvable as it COULD be a bug in the device as well => if someone knows of such a bug, telling me would be fine as well
  • I don't want to use a dialog (or anything else) instead, that's not answering my question. I currently use a BottomSheet which is fine for me, but still I would like to know if the problem can be solved and somehow handled

回答1:


I got the same problem on a Nexus 7 (not 2012) running the 5.1.1. It is finally fixed by adding this line:

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



回答2:


In my case, popup window have no sizes on few devices.

Try that after setContentView

50000 - just a big size for measure.

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

You can use screen size instead 50000




回答3:


I was having the same problem: my PopupWindow was not showing on my 5.1.1 android device but it was on others. I realised that I had to specify the width and height in order to be shown on that version (which is still compatible with the rest of the versions as well).

Here is an example:

popUp.setWidth(MATCH_PARENT);
popUp.setHeight(WRAP_CONTENT);



回答4:


Some ROMs restrict usage of popupview with their own permissions. So user have to explicitly turn on permission to show pop up views.

Even MIUI will restrict popupview to be displayed by default.

Please have a look whether there is any permission in that ROMs or devices.




回答5:


Try QuickAction

Activity (ExampleActivity1.java) to show how to use 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);
            }
        });
    }
}

Output:


(source: unitid.nl)




回答6:


## Ok i implemented popupwindow for sorting in my tab fragment and i checked working fine once try this

I used in that custom layout for popup window

  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);

Find the below attached screen shot popupwindow in my app




回答7:


I had created a custom popup dialog shows at bottom of screen

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();
}
}

And style is here

<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>

And this dialog working in all devices :)



来源:https://stackoverflow.com/questions/43331748/popupwindow-not-working-on-a-few-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!