简体   繁体   English

无法将图标添加到操作栏

[英]Unable to add icons to action bar

I have recently started a new project and created a custom action bar but for some reason it is not adding icons to it, they will appear on the overflow part but not on the actual bar! 我最近开始了一个新项目,并创建了一个自定义操作栏,但是由于某种原因它没有在其中添加图标,因此它们会出现在溢出部分,而不是实际的栏中! :(, really sorry if this seems simple I have just started learning android, I'll post my code below. :(,真的很抱歉,如果这看起来很简单,我刚开始学习android,我将在下面发布我的代码。

menu_main.xml menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"   
tools:context=".MainActivity">

<item android:id="@+id/maps"
    android:icon="@drawable/ic_action_map"
    android:title= "@string/maps"
    app:showAsAction="ifRoom"
    android:orderInCategory="100"/>

<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never" />

part of my main method 我主要方法的一部分

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

Thanks in advance 提前致谢

edit: I'm using android studio as well 编辑:我也在使用android studio

edit2: Here is my manifest file. edit2:这是我的清单文件。

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_maps"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
   >
    <activity
        android:name=".MainActivity"
        android:label="SIX REP MAX" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Chest"
        android:parentActivityName=".MainActivity">
    </activity>
    <activity android:name=".Back"
        android:parentActivityName=".MainActivity">
    </activity>
    <activity android:name=".Biceps"
        android:parentActivityName=".MainActivity">
    </activity>
    <activity android:name=".Triceps"
        android:parentActivityName=".MainActivity">
    </activity>
    <activity android:name=".Shoulders"
        android:parentActivityName=".MainActivity">
    </activity>
    <activity android:name=".Legs"
        android:parentActivityName=".MainActivity">
    </activity>
</application>

edit 3: Here is my custom action bar 编辑3:这是我的自定义操作栏

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#0099FF</item>
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>

</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">24sp</item>
</style>

Do this for all menu items: 对所有菜单项执行此操作:

app:showAsAction="always"

and add this to your Activity : 并将其添加到您的Activity

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    // This will hide the 3-dots menu
    return false;
}

Try this. 尝试这个。 This will work. 这将起作用。

This is the code you should be using , careful look at the return in onCreateOptionsMenu 这是您应该使用的代码,仔细查看onCreateOptionsMenu中的返回值

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.some_menu_xml, menu);

   //return true is important here
    return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.maps:
            //Do Something
            return true;
        case android.R.id.home:
           //Do Something else
            return true;
    }
    return super.onOptionsItemSelected(item);
}

Try the following code: 尝试以下代码:

    <item android:id="@+id/maps"
    android:icon="@drawable/ic_action_map"
    android:title= "@string/maps"
    app:showAsAction="always"
    android:orderInCategory="100"/>
   <item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="always" />

Probably your image is not fitting to actionbar. 您的图片可能不适合动作栏。 So as you are trying 所以当你尝试

app:showAsAction="ifRoom"

Its misplacing. 其错位。 So instead of using ifRoom put always. 因此,不要总是使用ifRoom。 Otherwise try to make the image resized to fit your action bar 否则,请尝试调整图像大小以适合您的操作栏

Your code seems all okay but try removing 您的代码似乎还可以,但是请尝试删除

xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"

from <menu> tag and also remove <menu>标记中删除

android:orderInCategory="100"

from <item android:id="@+id/maps" 来自<item android:id="@+id/maps"

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

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