如何用图标创建菜单?

时间:2013-03-20 03:19:31

标签: android menu-items

我真的需要一些关于如何使用图标创建菜单的指南或建议。

我正在写一个Android应用程序,我想创建一个这样的菜单,当我进入这个应用程序时,我可以在中心看到“主”菜单,我可以编辑菜单的文本。我按下主菜单图标,“+”图标滑出,然后我可以按“+”添加子菜单。下次,我进入这个应用程序,我可以看到主菜单图标周围有一些子菜单图标。如果我想,我可以按主菜单图标隐藏子菜单图标。(我无法发布图像,所以我希望你能理解)

真的需要帮助。

enter image description here

1 个答案:

答案 0 :(得分:3)

使用此代码在android中制作菜单。还可以从Android™ Drawables

下载这些图标
  

RES /布局/ menu.xml文件

         

<item android:id="@+id/menu_save"
      android:icon="@drawable/icon_save"
      android:title="Save" />

<item android:id="@+id/menu_search"
      android:icon="@drawable/icon_search"
      android:title="Search" />

<item android:id="@+id/menu_share"
      android:icon="@drawable/icon_share"
      android:title="Share" />

<item android:id="@+id/menu_delete"
      android:icon="@drawable/icon_delete"
      android:title="Delete" />  

<item android:id="@+id/menu_preferences"
      android:icon="@drawable/icon_preferences"
      android:title="Preferences" />

  

AndroidMenusActivity.java

package com.androidhive.androidmenus;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class AndroidMenusActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    // Initiating Menu XML file (menu.xml)
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }

    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_save:
            Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_search:
            Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_share:
            Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_delete:
            Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_preferences:
            Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }    

}

enter image description here