如何在Android中单击操作栏中的按钮时显示下拉菜单

时间:2016-01-15 06:59:59

标签: android android-actionbar dropdown

我的操作栏中有两个按钮:注销和编辑个人资料。当我点击编辑个人资料时,我想显示下拉菜单,但我不知道如何。请帮忙。提前谢谢!

public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.action_logout:
            AlertDialog.Builder builder=new AlertDialog.Builder(AdminHome.this);
            builder.setMessage("Are you sure you want to logout?")
            .setPositiveButton("Logout", new DialogInterface.OnClickListener() 
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    new Logs().execute();
                    finish();
                    Intent in=new Intent(AdminHome.this, MainActivity.class);
                    startActivity(in);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {

                }
            })
            .show();
            return true;

        case R.id.edituser:

            //WHAT TO PUT IN HERE
            return true;
    }

    return super.onOptionsItemSelected(item);

}

3 个答案:

答案 0 :(得分:0)

只需创建您的菜单项并点击它即可

CREATE OR REPLACE TRIGGER tri_order
 AFTER INSERT
 ON customer
 FOR EACH ROW
BEGIN
 INSERT INTO orders
           (o_id)
  SELECT :NEW.ID
  FROM   customer;
END;

对于nore deatil,请参阅此链接Android Show DropDown Menu on MenuItem click

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

    <item
       android:id="@+id/menu_item_action_parameters"
       android:title="@string/text_parameters"
       android:icon="@drawable/ic_menu_parameter"
       app:showAsAction="ifRoom|withText"/> >
       <menu>
          <item 
            android:id="@+id/action_dropdown1"
            android:title="@string/dropdown_1" />
          <item 
            android:id="@+id/action_dropdown2"
            android:title="@string/dropdown2" />
          <item 
            android:id="@+id/action_dropdown3"
            android:title="@string/dropdown3" />
        </menu>
    </item>

    <item
      more item
    </item>
</menu>

答案 1 :(得分:0)

以这种方式执行此操作,您的菜单xml应该是这样的,您要在操作栏或工具栏上显示的项目应放在父菜单标签和子菜单项中(当edituser是时,它将显示在下拉菜单中) tapped)应放在另一个菜单标签中:

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

 <item android:id="@+id/action_logout"
      android:icon="@drawable/action_logout"
      android:title="@string/action_logout"
      android:showAsAction="always" > 

<item android:id="@+id/edituser"
      android:icon="@drawable/edituser"
      android:title="@string/edituser"
      android:showAsAction="always" >
 <menu>
 <item android:id="@+id/item1"
      android:icon="@drawable/ic_launcher"
      android:title="@string/item1"
      android:showAsAction="never"
      />

<item android:id="@+id/item2"
      android:icon="@drawable/ic_launcher"
      android:title="@string/item2" 
      android:showAsAction="never" />


<item android:id="@+id/item3"
      android:icon="@drawable/ic_launcher"
      android:title="@string/item3" 
      android:showAsAction="never"/>


<item android:id="@+id/item4"
      android:icon="@drawable/ic_launcher"
      android:title="@string/item4"
      android:showAsAction="never" />

 </menu>
      </item>
 </menu>

我希望我的回答清楚。希望它会对你有所帮助。

答案 2 :(得分:0)

下面是示例menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_logout" 
    android:icon="@drawable/logout"
    android:title="Logout"
    app:showAsAction="always"
    android:orderInCategory="1" />

<item
    android:id="@+id/action_edtprofile"
    android:icon="@drawable/edtProfile"
    android:title="Edit Profile"
    app:showAsAction="always"
    android:orderInCategory="0">

    <menu>
        <group>

            <item 
                 android:id="@+id/action_dropdown_item1"
                 android:title="dropdown_item1" />

            <item 
                 android:id="@+id/action_dropdown_item2"
                 android:title="dropdown_item2" />

            <item 
                 android:id="@+id/action_dropdown_item3"
                 android:title="dropdown_item3" />

        </group>
    </menu>

</item>

onClick of Edit Profile一个dropdon将出现Sub-Menus并使用id你可以执行操作。