如何在Android溢出按钮来改变弹出菜单的位置?弹出、按钮、菜单、位置

2023-09-12 02:23:20 作者:成绩总挑拨我和父母的关系

我只是想实现出头一样弹出的Gmail应用程序菜单,挂靠溢出按钮右上角。为我用同样的code作为谷歌的教程为Android 的Andr​​oid弹出菜单,但对我表现出对不属于该动作条的边缘之上弹出菜单。如果您发现的Gmail溢出的弹出菜单中,你看到popmenu发生在动作条的边缘。

这是我用来弹出菜单中的xml:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <项目
        机器人:ID =@ + ID / ITEM1
        机器人:标题=扁豆/>
    <项目
        机器人:ID =@ + ID / ITEM2
        机器人:标题=扁豆/>

< /菜单>
 

,并在后续的在我的活动:

 公共无效showFontSetting(查看视图){
    PopupMenu的弹出=新PopupMenu的(这一点,视图);
    MenuInflater充气= popup.getMenuInflater();
    inflater.inflate(R.menu.menu,popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(新PopupMenu.OnMenuItemClickListener(){

        @覆盖
        公共布尔onMenuItemClick(菜单项项){
        // TODO自动生成方法存根

            开关(item.getItemId()){
                案例R.id.item1:
                    Toast.makeText(Index.this,
                        你点击+ item.getTitle()
                    Toast.LENGTH_SHORT).show();
                    打破;
                案例R.id.item2:
                    打破;
            }
            返回true;
        }
    });
}
 
android 的Button怎样自动触发按下的动作

解决方案

添加下面这段code到你的活动:

  PopupWindow popupwindow_obj; //创建对象

popupwindow_obj = popupDisplay(); //初始化的onCreate()

popupwindow_obj.showAsDropDown(clickbtn,-40,18); //其中u要显示在视图中单击事件

公共PopupWindow popupDisplay(){// DISPLY设计你popoup窗口
    最后PopupWindow popupWindow =新PopupWindow(本); // inflet您的布局或diynamic添加视图

    查看图。

    LayoutInflater充气=(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。

    鉴于= inflater.inflate(R.layout.mylayout,NULL);

    Button项=(的LinearLayout)view.findViewById(R.id.button1);

    popupWindow.setFocusable(真正的);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(视图);

    返回popupWindow;
}
 

在创建 RES /布局XML 目录并将其命名为 mylayout.xml

 <的LinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=横向>

        <按钮
            机器人:ID =@ + ID /按钮1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=窗口测试/>
< / LinearLayout中>
 

I just like to implement somethings same as popup menu in the Gmail app, anchored to the overflow button at the top-right. for that I used the same code as google tutorial for android Android popup menu, but for me show pop menu on top of edge of actionbar not under that. If you notice on pop menu of gmail overflow you saw that popmenu take place at edge of actionbar.

This is the xml that I used for popup menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item1"
        android:title="lablab"/>
    <item
        android:id="@+id/item2"
        android:title="lablab"/>

</menu>

and at the follow is in my activity:

public void showFontSetting(View view) {
    PopupMenu popup = new PopupMenu(this, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
        // TODO Auto-generated method stub

            switch (item.getItemId()) {
                case R.id.item1:
                    Toast.makeText(Index.this,
                        "You Clicked : " + item.getTitle(),
                    Toast.LENGTH_SHORT).show();
                    break;
                case R.id.item2:
                    break;
            }
            return true;
        }
    });
}

解决方案

Add the following piece of code to your activity:

PopupWindow popupwindow_obj; // create object

popupwindow_obj=popupDisplay();  // initialize in onCreate()

popupwindow_obj.showAsDropDown(clickbtn,-40, 18); // where u want show on view click event

public PopupWindow popupDisplay() { // disply designing your popoup window
    final PopupWindow popupWindow = new PopupWindow(this); // inflet your layout or diynamic add view

    View view; 

    LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    view = inflater.inflate(R.layout.mylayout, null);

    Button item = (LinearLayout) view.findViewById(R.id.button1);

    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);

    return popupWindow;
}

Create an XML in res/layout directory and name it mylayout.xml

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Window test" />
</LinearLayout>