简体   繁体   English

水平弹出菜单?

[英]Horizontal Pop up Menu?

I am trying to make share button when clicked a Popupmenu appears with three horizontal choices facebook , twitter and google+ .当点击一个Popupmenu时,我试图制作共享按钮, Popupmenu出现三个水平选项facebooktwittergoogle+

I keep searching for a while but I got nothing until now.我一直在寻找一段时间,但直到现在我一无所获。

Is it possible to create horizontal or even grid PopupMenu ?是否可以创建水平甚至网格PopupMenu Is it possible to use RecyclerView in PopupMenu ?是否可以在PopupMenu使用RecyclerView

除了PopupMenu ,您是否能够使用具有自定义布局的DialogFragment

In this case, you can use PopupWindow .在这种情况下,您可以使用PopupWindow You can use ListView to display items in each line.您可以使用ListView来显示每行中的项目。 Example code to show a PopupWindow :显示PopupWindow示例代码:

 public PopupWindow popupWindowsort() {

    // initialize a pop up window type
    popupWindow = new PopupWindow(this);

    ArrayList<String> sortList = new ArrayList<String>();
    sortList.add("Google+");
    sortList.add("Facebook");
    sortList.add("Twitter");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, sortList);
    // the drop down list is a list view
    ListView listViewSort = new ListView(this);

    // set our adapter and pass our pop up window contents
    listViewSort.setAdapter(adapter);

    // set on item selected
    listViewSort.setOnItemClickListener(onItemClickListener());

    // some other visual settings for popup window
    popupWindow.setFocusable(true);
    popupWindow.setWidth(250);
    //popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

    // set the list view as pop up window content
    popupWindow.setContentView(listViewSort);

    return popupWindow;
}

Visit my post for more details: http://www.devexchanges.info/2015/02/android-popupwindow-show-as-dropdown.html .访问我的帖子了解更多详情: http : //www.devexchanges.info/2015/02/android-popupwindow-show-as-dropdown.html
Hope this help!希望这有帮助!

Create a basic layout XML with an inner horizontal layout.创建具有内部水平布局的基本布局 XML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/popup_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
</LinearLayout>

Assign the layout to the PopupMenu, then use the inner layout.将布局分配给 PopupMenu,然后使用内部布局。

// PopupMenu accepts ViewGroup, so cast the inflated layout view
// Replace ROOT_VIEW_HERE with the parent view of the PopupMenu
LinearLayout popupWindow = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.popup_window, ROOT_VIEW_HERE);
PopupMenu popupMenu = new PopupMenu(this, popupWindow);
LinearLayout innerView = popupWindow.findViewById(R.id.popup_horizontal);

It really is that simple.真的就是这么简单。

A complete solution is:一个完整的解决方案是:

1 - The Custom PopupMenu class: 1 - 自定义 PopupMenu 类:

public class PopupMenuCustomLayout {
    private PopupMenuCustomOnClickListener onClickListener;
    private Context context;
    private PopupWindow popupWindow;
    private int rLayoutId;
    private View popupView;

    public PopupMenuCustomLayout(Context context, int rLayoutId, PopupMenuCustomOnClickListener onClickListener) {
        this.context = context;
        this.onClickListener = onClickListener;
        this.rLayoutId = rLayoutId;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        popupView = inflater.inflate(rLayoutId, null);
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true;
        popupWindow = new PopupWindow(popupView, width, height, focusable);
        popupWindow.setElevation(10);

        LinearLayout linearLayout = (LinearLayout) popupView;
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            View v = linearLayout.getChildAt(i);
            v.setOnClickListener( v1 -> { onClickListener.onClick( v1.getId()); popupWindow.dismiss(); });
        }
    }
    public void setAnimationStyle( int animationStyle) {
        popupWindow.setAnimationStyle(animationStyle);
    }
    public void show() {
        popupWindow.showAtLocation( popupView, Gravity.CENTER, 0, 0);
    }

    public void show( View anchorView, int gravity, int offsetX, int offsetY) {
        popupWindow.showAsDropDown( anchorView, 0, -2 * (anchorView.getHeight()));
    }

    public interface PopupMenuCustomOnClickListener {
        public void onClick(int menuItemId);
    }
}

2 - Your custom layout, eg linearlayout with horizontal layout. 2 - 您的自定义布局,例如具有水平布局的线性布局。 In this case I use a simple LinearLayout with TextView items.在这种情况下,我使用带有 TextView 项目的简单 LinearLayout。 You can use Buttons, etc.您可以使用按钮等。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/popup_menu_custom_item_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="A"
        android:textAppearance="?android:textAppearanceMedium" />
    <TextView
        android:id="@+id/popup_menu_custom_item_b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="B"
        android:textAppearance="?android:textAppearanceMedium" />
    // ...
</LinearLayout>

3 - Using the Custom PopupMenu like the normal PopupMenu. 3 - 像普通的 PopupMenu 一样使用自定义 PopupMenu。

PopupMenuCustomLayout popupMenu = new PopupMenuCustomLayout(
        MainActivity.mainActivity, R.layout.popup_menu_custom_layout,
        new PopupMenuCustomLayout.PopupMenuCustomOnClickListener() {
            @Override
            public void onClick(int itemId) {
                // log statement: "Clicked on: " + itemId
                switch (itemId) {
                    case R.id.popup_menu_custom_item_a:
                        // log statement: "Item A was clicked!"
                        break;
                }
            }
        });
// Method 1: popupMenu.show();
// Method 2: via an anchor view: 
popupMenu.show( anchorView, Gravity.CENTER, 0, 0);

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

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