1

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

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

真的需要帮助。

在此处输入图像描述

4

1 回答 1

3

使用此代码在 android 中制作菜单。还可以从Android™ Drawables下载这些图标

资源/布局/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);
        }
    }    

}

在此处输入图像描述

于 2013-03-20T04:21:22.387 回答