【问题标题】:Position, Shadow and dismissing of PopupWindow like an Actionbar Menu [Android]PopupWindow 的位置、阴影和关闭,如操作栏菜单 [Android]
【发布时间】:2013-10-12 14:22:04
【问题描述】:

我正在尝试创建一个 PopupWindow,其工作方式类似于 Android 版 Chrome 中的操作栏菜单 - 例子: How to create custom sub menu like Google Chrome application

这是我目前所拥有的:

目前 - 我有 3 个问题:

  1. 我需要将菜单定位到右侧的操作栏按钮及其下方。像通常的菜单一样..

  2. 我需要在 PopupWindow 上有一个阴影(由于某种原因它被移除了,因为我知道 PopupWindow 周围自动有这个阴影)

  3. 在 PopupWindow 外部单击时,我需要它像操作栏的常用菜单一样关闭。

这是我的代码- 单击操作栏按钮时,这是运行的代码:

LayoutInflater inflater = (LayoutInflater) MainActivity.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final PopupWindow pw = new PopupWindow(inflater.inflate(
            R.layout.options_menu, null, false), LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
    pw.showAtLocation(findViewById(R.id.main_cont), Gravity.NO_GRAVITY, 50, this.getSupportActionBar().getHeight() + 1);


    ListView moreOptions = (ListView)pw.getContentView().findViewById(R.id.moreOptions);
    String[] options = new String[] { "Settings", "Feedback", "Contribute", "DashClock Integration", "Tutorial", "ViewPager Demo", "Expandable List Demo", "Change Language", "Add 5 to list", "Add 20 to list" };

    final ArrayList<String> moreOptionsList = new ArrayList<String>();
    for (int i = 0; i < options.length; ++i) {
        moreOptionsList.add(options[i]);
    }

    ArrayAdapter<String> moreOptionsAdapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            moreOptionsList);
    moreOptions.setAdapter(moreOptionsAdapter);


    //pw.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.listview_bg_inverted));
    /*
    pw.setTouchable(true);
    pw.setFocusable(false);
    */
    pw.setOutsideTouchable(true);

菜单设计: http://pastebin.com/yi6S2HDt

提前致谢, 丁。

【问题讨论】:

    标签: android position shadow popupwindow


    【解决方案1】:

    PopupWindow 有一个背景(您可以轻松更改它),但它本身没有阴影。

    您实际使用的showAtLocation() 允许您指定窗口出现的确切位置。

    要让它自动关闭,只需在构造函数中调用setOutsideTouchable(true);

    但是说了这么多,用PopupMenu 代替PopupWindow 不是更好吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      相关资源
      最近更新 更多