The application icon does not show on action bar

前端 未结 6 1766
暗喜
暗喜 2020-11-29 02:06

I followed up the instructions of building a new android project and I got a runnable one except a problem with action bar. The problem is that the application icon is not s

相关标签:
6条回答
  • 2020-11-29 02:19

    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);
    
    0 讨论(0)
  • 2020-11-29 02:29

    You are using the AppCompat version 21+ and it is normal.

    The Action Bar follows the material design guidelines and uses a Toolbar. As you can read here:

    The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

    If you would like an application icon (but I discourage it), you can use the method setLogo().

    Something like this:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setLogo(R.drawable.my_logo);
    actionBar.setDisplayUseLogoEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
    
    0 讨论(0)
  • 2020-11-29 02:29

    Attribute logo is only used in API level 11 and higher (current min is 8), I was also confused about this question, maybe google just don't want the icon to show on material design, when the minimum sdk is set to 14 or higher and under 21,it uses holo theme, it has an icon, but appcompat style is more like material design I think, maybe google just forget to modify the holo theme

    0 讨论(0)
  • 2020-11-29 02:32

    Make sure you have the icon set in the manifest.xml file, in the application tag as:

        android:icon="@drawable/launcher_icon"
    

    Then in the onCreate method insert the following lines:

        ActionBar ab =getSupportActionBar(); 
        ab.setDisplayShowHomeEnabled(true);
        ab.setIcon(R.drawable.launcher_icon);
    
    0 讨论(0)
  • 2020-11-29 02:43

    This issue comes when you use support library revised 21.

    Use:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setLogo(R.drawable.ic_launcher);
    actionBar.setDisplayUseLogoEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
    

    It worked for me or you can use toolbar. A Toolbar is a generalization of action bars for use within application layouts.

    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.

    Reference: https://developer.android.com/reference/android/support/v7/widget/Toolbar.html

    0 讨论(0)
  • 2020-11-29 02:44

    This is a common "problem".Before you pull your hairs out make sure you are using:

    import android.support.v7.app.ActionBar; //or the v4, haven't tried with that though
    

    and not:

    import android.app.ActionBar;
    

    and then:

    ActionBar actionBar;
    actionBar = getSupportActionBar();
    

    instead of:

    actionBar = getActionBar();

    And finally:

    if (actionBar != null) {
            // enabling action bar app icon and behaving it as toggle button           
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setIcon(R.drawable.your_icon);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
        }
    
    0 讨论(0)
提交回复
热议问题