布局中的Android菜单选项不显示图标

时间:2013-07-15 14:11:03

标签: android android-layout android-menu

我刚刚为我的活动添加了一个菜单。另外我想为每个菜单项设置一些图标。因此,我尝试了这两种方法,但我没有在设备上看到这些图标。我做错了什么?

我尝试了XML方式:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/help"
    android:icon="@drawable/help"
    android:title="@string/menu_help" />
    <item android:id="@+id/settings"
    android:icon="@drawable/settings"
    android:title="@string/menu_settings" />
    <item android:id="@+id/num"
    android:icon="@drawable/num_icon"
    android:title="@string/menu_num" />
</menu>

方法2: 在我的代码中:

public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        //Set icon for the menu button
        Drawable num_icon = getResources().getDrawable(R.drawable.num_icon);
        menu.getItem(2).setIcon(num_icon);
        Drawable settings_icon = getResources().getDrawable(R.drawable.settings);
        menu.getItem(1).setIcon(settings_icon);
        Drawable help_icon = getResources().getDrawable(R.drawable.help);
        menu.getItem(0).setIcon(help_icon);
        return true;

另外,有谁能告诉我这些图标尺寸应该有多大?我是256x256像素。感谢

3 个答案:

答案 0 :(得分:1)

在android sdk菜单中,图标大小如下:

ldpi 32 x 32 px
mdpi: 36 x 36 px
hdpi: 48 x 48 px
xhdpi: 64 x 64 px

256x256太大了。

答案 1 :(得分:1)

使用以下代码可能对您有所帮助.. menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Single menu item 
         Set id, icon and Title for each menu item
    -->
    <item android:id="@+id/menu_bookmark"
          android:icon="@drawable/icon_bookmark"
          android:title="Bookmark" />

    <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" /> 
</menu>

现在打开您的主Activity类文件并输入以下代码 MenusActivity.java

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);
        }
    }    

}

答案 2 :(得分:1)

你只需要根据你的需要对你的清单文件进行微小的改动你可以改变主题我得到解决上述问题的小改变ieeven使用任何版本的android它将适用于(2.3到最新版本4.4 kitkat也)。

  <application
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         android:theme="@android:style/Theme.WithActionBar">