如何在带有标签图标的按钮上执行弹出菜单?

时间:2016-06-07 15:35:30

标签: android android-layout android-gridview

我正在尝试按照指南来实现这一目标: enter image description here

基本上我想要这样的东西:

enter image description here

我想让它看起来像是文档中的弹出窗口。

当用户点击“观看次数”时,系统会显示自定义视图,显示更多选项。我正在为PopupWindow创建一个自定义布局,但它没有给我我想要的结果。我正在创建一个简单的颜色选择器,它允许用户只选择8种颜色。我尝试了一个包含RecyclerView的布局,但它看起来像这样:

enter image description here

我知道这是不可能的,但有没有人能够实现" Google"在他们的导游上做广告?我相信这是不可能的,但许多应用程序都能够做到。

以下是我如何实例化弹出窗口的代码:

private void showChooseColorPopup()
{
    /*
    PopupMenu popup = new PopupMenu(this, mBottomMenuPanelLinearLayout);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_note_color, popup.getMenu());
    popup.show();
    */

    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.layout_popup_menu_note_color , null);

    NoteColorSelectionAdapter adapter = new NoteColorSelectionAdapter(this, NotifireID.colors);
    adapter.setOnColorSelected(this);

    GridLayoutManager layoutManager = new GridLayoutManager(this , 3);
    layoutManager.setOrientation(GridLayoutManager.HORIZONTAL);

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_popup_menu_note_color);
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);

    // TODO Use Dialog instead

    PopupWindow popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.ic_white));

    int location[] = new int[2];
    mColorButton.getLocationOnScreen(location);
    popupWindow.showAtLocation(mColorButton, Gravity.NO_GRAVITY, location[0] , location[1] - mColorButton.getHeight());
    // popupWindow.show(mColorButton);
}

这是我的GridView适配器:

package com.neonwarge.android.notifire.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.neonwarge.android.notifire.R;

import java.util.ArrayList;

public class NoteColorSelectionAdapter extends RecyclerView.Adapter<NoteColorSelectionAdapter.ViewHolder>
{
    private final static int COUNT = 8;
    private ArrayList<Integer> mColors;
    private Context mContext;
    private OnColorSelected mOnColorSelected;

    public interface OnColorSelected
    {
        public void onColorSelected(View v, int position, int color);
    }

    public NoteColorSelectionAdapter(Context context , ArrayList<Integer> noteColors)
    {
        mColors = noteColors;
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater inflater = LayoutInflater.from(mContext);

        View view = inflater.inflate(R.layout.listitem_popup_menu_note_color, parent, false);

        NoteColorSelectionAdapter.ViewHolder viewHolder = new NoteColorSelectionAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
        int selectedColor = mColors.get(position);
        final int p = position; final int color = selectedColor;

        switch(selectedColor)
        {
            case R.color.yellow:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_yellow));
                break;

            case R.color.orange:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_orange));
                break;

            case R.color.purple:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_purple));
                break;

            case R.color.red:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_red));
                break;

            case R.color.pink:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_pink));
                break;

            case R.color.skyblue:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_skyblue));
                break;

            case R.color.brown:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_brown));
                break;

            case R.color.green:
                holder.mColorImageButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_green));
                break;
        }

        holder.mColorImageButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(mOnColorSelected != null)
                    mOnColorSelected.onColorSelected(v, p, color);
            }
        });
    }

    @Override
    public int getItemCount()
    {
        return COUNT;
    }

    public void setOnColorSelected(OnColorSelected i)
    {
        mOnColorSelected = i;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public ImageButton mColorImageButton;

        public ViewHolder(View view)
        {
            super(view);

            mColorImageButton = (ImageButton) view.findViewById(R.id.imagebutton_color_note);
        }
    }
}

这是我的layout_popup_menu_note_color:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_popup_menu_note_color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

这是我检查过的链接,它们都不适用于我。除了反思黑客,因为我还没有尝试过。

PopupWindow in android

Set own layout in popup window in android

How to define layout in a PopupWindow from an xml file, when PopupWindow method is called from a separate class

PopupMenu适合我,但我想要图标。我知道这是不可能的事实。但我不同意,我看到很多应用程序在其布局上有一个按钮,我点击它然后会出现PopupWindow,使按钮看起来像菜单下拉列表。

我这样做的原因是因为你可以看到,我的底部面板菜单只是一个自定义布局。我创建了一个自定义标签按钮,我想在单击它时弹出一个菜单。

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您看到的主要问题是因为您已将PopupWindow的RelativeLayout设置为:

android:layout_width="match_parent"
android:layout_height="match_parent"

这就是为什么你的图标在整个视图中显示出来的原因所以它看起来并不像弹出窗口。

尝试更改以下内容以查看是否有帮助:

   android:layout_width="wrap_content"
   android:layout_height="wrap_content"