简体   繁体   English

操作栏的弹出窗口

[英]Pop-up window from actionbar

I'm trying to create a pop-up window in Android by clicking button in action bar. 我正在尝试通过单击操作栏中的按钮在Android中创建弹出窗口。 Like this: 像这样:

http://pix.am/yo2E.jpg 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. 在我的想法中,我意识到它在一个容器中有两个片段,其中1(弹出)处于View.GONE状态,当我点击按钮时变得可见。

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菜单文件

<?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 现在,编写PopupMenu1 Activity.java文件

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 showAsDropDown(findViewbyId(R.id.menuitem),0,0) to show as simple dropdown for that actionbar button. 编辑:现在你可以使用showAsDropDown(findViewbyId(R.id.menuitem),0,0)来显示该操作栏按钮的简单下拉列表。

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 我使用了showAtLocation(),因为你可以在屏幕的任何位置显示但是稍后我会谈到的小bug,这有点好,因为它将重力作为参数之一以及XY位置,所以用这个来在屏幕上你想要的地方

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 现在从活动中的OnOptionsItemSelected()启动窗口

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 这里的捕获是showatlocation() ,你的窗口适用于动作栏点击,但是setOutsideTouchable()使窗口也对窗口区域外的触摸做出反应,这意味着如果你触摸外面它会解散窗口,有一个叫做isShowing的方法()在这种情况下会有所帮助,但它有问题
在此输入图像描述

Take a look at official PopupMenu support from android. 看一下android的官方PopupMenu支持。 You can launch a popup window on button click and inflate your own UI for that window. 您可以在按钮单击时启动弹出窗口,并为该窗口充气您自己的UI。

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

If you want to support older versions of android, you can use PopupMenuCompat 如果要支持旧版本的android,可以使用PopupMenuCompat

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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