gpt4 book ai didi

android - 如何创建带有图标的菜单?

转载 作者:行者123 更新时间:2023-11-29 18:01:55 26 4
gpt4 key购买 nike

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

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

真的需要这方面的帮助。

enter image description here

最佳答案

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

res/layout/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

关于android - 如何创建带有图标的菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com