无法将 android 徽标放入操作栏

Trouble getting android logo into the action bar

我只想要 activity 名称旁边的清单 (ic_launcher.png) 中指定的徽标。它显示为启动器的图标,但不在菜单中。我已将徽标和图标属性指向清单中的这张图片。我在 OnCreateOptionMenu 中使用 getMenuInflater() 来填充屏幕顶部的菜单。那一直很好用。我对其进行了设置,以便用户可以单击并在应用程序的不同活动中移动。显示了应用程序的名称,但该栏中没有图标。

我的清单有标志:

 <application
        android:allowBackup="true"
        android:logo="@mipmap/ic_launcher"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

当我将此代码放入启用时,程序崩溃了。为什么?

    ActionBar actionBar= getActionBar();
    actionBar.setDisplayUseLogoEnabled(true);

On android's own developer site 它说:

Using a logo instead of an icon

By default, the system uses your application icon in the action bar, as specified by the icon attribute in the or element. However, if you also specify the logo attribute, then the action bar uses the logo image instead of the icon.

我也直接运行试过了,结果一样:

getActionBar().setDisplayUseLogoEnabled(true);

崩溃:

Caused by: java.lang.NullPointerException
at .MainActivity.onCreate(MainActivity.java:33)

指向上面的语句

我也试过:

getSupportActionBar().setDisplayUseLogoEnabled(true);

应用程序不会因此而崩溃,但没有徽标。

我将 Theme.AppCompat.Light.DarkActionBar 作为我的应用程序主题,我相信这会带走我的图标。

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

我正在扩展 ActionBarActivity 并扩充菜单 getMenuInflater().inflate(R.menu.menu_main, menu);

我正在扩展 ActionBarActivity。

android:logo="@drawable/...." 添加到清单文件的应用程序部分。

android:logo 整个应用程序的徽标,以及活动的默认徽标。

此属性必须设置为对包含图像的可绘制资源的引用(例如“@drawable/logo”)。没有默认徽标。

我还建议您使用 android:icon="@drawable/ic_launcher" 作为应用程序的代替。(尝试 @drawable )

如果不行,试试getActionBar().setDisplayShowHomeEnabled(true); (应该在 setContentView(R.layout.activity_main); 之前调用)

顺便说一句,如果你想为每个不同的Activity指定一个图标,你也可以设置android:logo="@drawable/YOUR_ICON"。 =23=] 在 Activity

希望有用

试试这个...

<style name="MyActionBarLogo" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="background">@color/mainColor500</item>
    <item name="logo">@drawable/actionbar_logo</item>
    <item name="displayOptions">useLogo|showHome</item>
</style>

这个问题很可能是因为您使用的是 21.+ 或 22.+ 版本的支持库。从 Lollipop 开始,不鼓励在 ActionBar 中使用应用程序徽标,最新版本的支持库反映了这一变化。

如果您仍希望在 ActionBar 中使用应用徽标,则需要将 Toolbar 视图添加到您的布局中(有关此视图的更多信息,请参见 here) and use one of its setLogo() methods: first or second

getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayUseLogoEnabled(true);

根据Android blogmipmap文件夹是放应用程序图标的,不是说应用程序logo也可以。所以我想你最好从 drawable 文件夹中设置徽标:

<application
    android:allowBackup="true"
    android:logo="@drawable/ic_launcher"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

希望对您有所帮助!

您的代码存在不止一个问题。

既然你用的是ActionBarActivity和AppCompat,那么你在使用ActionBarActivity的时候就得明白它到底是什么意思了。

您尚未发布代码,因此不清楚您使用的是 Toolbar 还是 ActionBar(ActionBar 已弃用,如果您仍在使用,则不应使用。)

如果您已经在使用工具栏,那么您可以像这样设置图标。

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        toolbar.setLogo(R.drawable.icon_here);
    }

如果您不使用工具栏,请先在您的主题中设置这些行。

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>

<!-- following two lines -->
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
</style>

然后在您的 activity 布局文件中,在顶部添加工具栏。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >