简体   繁体   English

图标未在Android应用程序中显示

[英]Icons are not displaying in Android application

I am complete newbie when it comes to Android applications. 关于Android应用程序,我是个新手。 Please bear with me. 请多多包涵。 I am trying to make an menu options bar but somehow the icons are not displaying. 我试图制作一个菜单选项栏,但是不知何故图标没有显示。

I had looked for answers to this question on stackoverflow and the answer that I came across was "On Android 3.0+, the preferred approach for the options menu (a spillover menu in the action bar) will not show icons. If you have android targetSdkVersion of 11 or higher, icons will never show up in menus on Android 3.0+. The icons will show up if you promote an options menu item to be a toolbar button, and the icons will show up on Android 1.x/2.x devices." 我一直在寻找有关stackoverflow的问题的答案,并且遇到的答案是“在Android 3.0+上,选项菜单(操作栏中的溢出菜单)的首选方法不会显示图标。如果您有android targetSdkVersion (共11个或更高)图标将永远不会显示在Android 3.0+的菜单中。如果将选项菜单项提升为工具栏按钮,则图标将显示,而图标将在Android 1.x / 2.x中显示。设备。” I am sorry but I am not sure what that means. 对不起,但是我不确定这意味着什么。 Can someone please direct me in the right direction? 有人可以指引我正确的方向吗? Any help will be appreciated! 任何帮助将不胜感激!

I am not sure what I am doing incorrectly. 我不确定自己在做什么错。 Here is my code so far. 到目前为止,这是我的代码。

MainActivity.java MainActivity.java

package com.example.useoptionsmenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.about:
    startActivity(new Intent(this, About.class));
    return true;
    case R.id.help:
    startActivity(new Intent(this, Help.class));
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
}

}

main.xml main.xml

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/about"
    android:showAsAction="always"
    android:orderInCategory="1"
    android:icon="@drawable/ic_action_about"
    android:title="About"/>
<item
    android:id="@+id/help"
    android:showAsAction="always"
     android:orderInCategory="2"
    android:icon="@drawable/ic_action_help"
    android:title="Help"/>

</menu>

AndroidManifest.xml AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.useoptionsmenu"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.useoptionsmenu.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.useoptionsmenu.About"
        android:label="@string/title_activity_about" >
    </activity>
    <activity
        android:name="com.example.useoptionsmenu.Help"
        android:label="@string/title_activity_help" >
    </activity>
</application>

</manifest>

You have to add this to the item you want to display in the header: 您必须将此添加到要显示在标题中的项目中:

    android:showAsAction="ifRoom"

so that your menu will be: 这样您的菜单将是:

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="ifRoom"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/about"
    android:showAsAction="ifRoom"
    android:orderInCategory="1"
    android:icon="@drawable/ic_action_about"
    android:title="About"/>
<item
    android:id="@+id/help"
    android:showAsAction="ifRoom"
     android:orderInCategory="2"
    android:icon="@drawable/ic_action_help"
    android:title="Help"/>

</menu>

remove the line android:showAsAction="ifRoom" from the item you won't display in your header. 从您不会在标题中显示的项目中删除行android:showAsAction =“ ifRoom”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM