Pop-up window from actionbar

*爱你&永不变心* 提交于 2019-12-24 03:04:40

问题


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?


回答1:


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




回答2:


EDIT:Now you Can use showAsDropDown(findViewbyId(R.id.menuitem),0,0) to show as simple dropdown for that actionbar button.

I used showAtLocation() 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 gravity as one of the parameter along with X and Y positions,so use this to place where ever you want on the screen

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 showatlocation(), your window works fine for actionbar clicks , but setOutsideTouchable() 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 isShowing() which would have helped in that case but it's buggy




回答3:


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



来源:https://stackoverflow.com/questions/21162627/pop-up-window-from-actionbar

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