No App Icon on ActionBar

2019-01-04 02:04发布

There are a lot of queries here about adding icons to ActionBar but none solved my problem. If you know a duplicate of this question, feel free to comment or close this question.

I migrated my project to IntelliJ and I didn't encounter this problem with my previous IDE (Eclipse).

PROBLEM: The app icon is not displayed in the ActionBar.

enter image description here

I think it's supposed to be added by default that's why I can't add it through its XML

enter image description here

Here's its 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/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="always" />
</menu>

Thanks!

6条回答
唯我独甜
2楼-- · 2019-01-04 02:16

It really works. Try this.. Include these lines to your onCreate method,

ActionBar menu = getSupportActionBar();
menu.setDisplayShowHomeEnabled(true);
menu.setIcon(R.mipmap.jerry);

Also, make sure that jerry is a .png image file available under mipmap folder. To get the icon exactly at the start of your action bar, have jerry file in all screen sizes like (hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi etc) in mipmap folder.

查看更多
爷、活的狠高调
3楼-- · 2019-01-04 02:16

Setting Icon On the Action Bar

Showing Icon On the Action Bar Can Easily trick any developer in trouble. Keep in Mind this Simple Method and it will never make you in trouble.

If Your activity is extending Activity,

getActionBar().setDisplayShowHomeEnabled(true);

If Your activity is extending AppCompatActivity,

ActionBar actionBar= getSupportActionBar();
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);

It always work for us,Keep it in Mind.

查看更多
放我归山
4楼-- · 2019-01-04 02:17

As of AppCompat version 21, the Action Bar follows the material design guidelines and uses a Toolbar:

  • A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

However, if you want an application icon, setLogo() is the correct method.

查看更多
相关推荐>>
5楼-- · 2019-01-04 02:20

IN Style.xml

 <style name="MyTheme_ActionBar" parent="@style/Theme.AppCompat.Light">
   <item name="icon">@drawable/actionbar_logo</item>
  </style>

In activity add this and try

ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
查看更多
霸刀☆藐视天下
6楼-- · 2019-01-04 02:31

If you don't care about the Material theme and are fine having an Activity that looks more JellyBean/Kitkat style and includes the icon in the Action Bar, you can do the following:

First, change themes setting in styles.xml from this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

To this:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

Now change all your Activities to inherit from android.app.Activity instead of android.support.v7.app.ActionBarActivity. That is:

Change this:

import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity{

To this:

import android.app.Activity;

public class MainActivity extends Activity {

The end of result of doing both of the above steps is that the icon as specified by the android:icon attribute in AndroidManifest.xml will appear in the Action Bar.

查看更多
劳资没心,怎么记你
7楼-- · 2019-01-04 02:33

Update your onCreate() method with the code below.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.drawable.ic_launcher);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    setContentView(R.layout.activity_main);
}

It worked for me. note: ic_launcher is the icon you want to display in your actionbar, for that you will have to add the icon in the drawable folder of your app project.

查看更多
登录 后发表回答