【问题标题】:How to add an icon to the ActionBar如何将图标添加到 ActionBar
【发布时间】:2015-03-09 11:30:51
【问题描述】:

我正在尝试为整个应用程序向我的 ActionBar 添加一个图标。

我在清单中的 Appication 标签中引用了 CustomActionBarTheme。

我的操作栏变成蓝色,但没有图标。

有人知道为什么吗?

谢谢

[重复] show icon in actionbar/toolbar with AppCompat-v7 21

<application
        android:name=".NfcScannerApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme"
        android:debuggable = "true" >

.

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

<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@color/cf_blue</item>
        <item name="android:icon">@drawable/ic_launcher</item>

        <!-- Support library compatibility -->
        <item name="background">@color/cf_blue</item>
        <item name="icon">@drawable/ic_launcher</item>
    </style>
</resources>

【问题讨论】:

标签: android android-theme


【解决方案1】:
Objects.requireNonNull(getSupportActionBar()).setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher_round);

【讨论】:

  • 不需要文档,效果很好
【解决方案2】:
ActionBar ab=getActionBar();
ab.setIcon(R.drawable.ic_launcher)

【讨论】:

  • 你能在你的答案周围添加更多信息吗?
  • 我正在使用支持库,所以我尝试了 ActionBar actionBar = getSupportActionBar(); actionBar.setIcon(R.drawable.ic_launcher);但它不起作用
猜你喜欢
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
相关资源
最近更新 更多