简体   繁体   中英

Pop-up window from actionbar

I'm trying to create a pop-up window in Android by clicking button in action bar. Like this:

http://pix.am/yo2E.jpg

In my idea I realised it with two fragments in one container, where 1 (pop-up) is in View.GONE state and becomes visible when I click button.

Is there an easier way to solve my problem?

Basically you can do that with very small amount of code and you will end up like this

but if you want to customize you have to design a custom layout

在此输入图像描述

To Achieve that, create a xml menu file like below

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"/>
    <item
        android:id="@+id/add"
        android:icon="@android:drawable/ic_menu_add"
        android:title="Add"/>
    <item
        android:id="@+id/edit"
        android:icon="@android:drawable/ic_menu_edit"
        android:title="Edit">
        <menu>
            <item
                android:id="@+id/share"
                android:icon="@android:drawable/ic_menu_share"
                android:title="Share"/>
        </menu>
    </item>

</menu>

Now, write PopupMenu1 Activity.java file

package com.example.popuptest;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

public class PopupMenu1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_menu_1);
    }

    public void onPopupButtonClick(View button) {
        PopupMenu popup = new PopupMenu(this, button);
        popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(PopupMenu1.this,
                        "Clicked popup menu item " + item.getTitle(),
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        popup.show();
    }
}

source

EDIT:Now you Can use to show as simple dropdown for that actionbar button. 来显示该操作栏按钮的简单下拉列表。

I used because you can display in any location of the screen but with small bug which I will talk about later, this is kinda good cause it takes as one of the parameter along with and positions,so use this to place where ever you want on the screen 因为你可以在屏幕的任何位置显示但是稍后我会谈到的小bug,这有点好,因为它将作为参数之一以及位置,所以用这个来在屏幕上你想要的地方

Oh and the action button wont work yet after click action,so use these values to make popup window react to actions outside the screen
popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setOutsideTouchable(true); now launch the window from OnOptionsItemSelected() inside your activity

The catch here is with , your window works fine for actionbar clicks , but makes the window also to react for touches outside the window area,meaning it dismisses the window if u touch outside, there is a method called which would have helped in that case but it's buggy ,你的窗口适用于动作栏点击,但是使窗口也对窗口区域外的触摸做出反应,这意味着如果你触摸外面它会解散窗口,有一个叫做的方法在这种情况下会有所帮助,但它有问题
在此输入图像描述

Take a look at official PopupMenu support from android. You can launch a popup window on button click and inflate your own UI for that window.

Link: http://developer.android.com/reference/android/widget/PopupMenu.html

If you want to support older versions of android, you can use PopupMenuCompat

Link: http://developer.android.com/reference/android/support/v7/widget/PopupMenu.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM