Android 使用 AppCompat 在 Actionbar 中不显示徽标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26472417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:33:17  来源:igfitidea点击:

Logo are not displayed in Actionbar, using AppCompat

androidandroid-appcompat

提问by zkminusck

I don't know where I missed something, but I can't set my logo in my actionbar. My AndroidManifest.xml:

我不知道我在哪里遗漏了什么,但我无法在我的操作栏中设置我的徽标。我的 AndroidManifest.xml:

<application 
    android:icon="@drawable/application_icon" 
    android:label="@string/icon_name"
    android:logo="@drawable/actionbar_logo"
    android:theme="@style/PolarTheme">

And also I set my logo in my activity tag in the manifest:

我还在清单的活动标签中设置了我的标志:

<activity 
    android:name=".Main"
    android:theme="@style/PolarThemeLogo"
    android:logo="@drawable/actionbar_logo"
    android:windowSoftInputMode="adjustPan">
</activity>

This is in my themes.xml:

这是在我的 themes.xml 中:

<style 
    name="PolarThemeLogo" parent="Theme.AppCompat.Light.DarkActionBar">  
    <item name="colorPrimary">@color/mainColor500</item>
    <item name="colorPrimaryDark">@color/mainColor700</item>
    <item name="colorAccent">@color/accentColorA200</item>

    <item name="actionBarStyle">@style/MyActionBarLogo</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

My styles.xml:

我的styles.xml:

<style name="MyActionBarLogo" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@color/mainColor500</item>
    <item name="displayOptions">useLogo</item>
</style>

In my Main.java:

在我的 Main.java 中:

public class Main extends ActionBarActivity {

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("");
    actionBar.setDisplayUseLogoEnabled(true);

Any ideas?

有任何想法吗?

UPDATE - SOLVED

更新 - 已解决

I removed all the android:logoattributes from my AndroidManifest.xml and I modified my MyActionBarLogo style like this:

android:logo从 AndroidManifest.xml 中删除了所有属性,并像这样修改了 MyActionBarLogo 样式:

<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>

Now the actionbar is displaying my logo. :)

现在操作栏正在显示我的徽标。:)

采纳答案by zkminusck

I removed all the android:logo attributes from my AndroidManifest.xml and I modified my MyActionBarLogo style like this:

我从 AndroidManifest.xml 中删除了所有 android:logo 属性,并像这样修改了 MyActionBarLogo 样式:

<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>

Now the actionbar is displaying my logo. :)

现在操作栏正在显示我的徽标。:)

回答by alijandro

I use the following code to show the logo, hope it would be helpful.

我使用以下代码来显示徽标,希望对您有所帮助。

getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.logo);

回答by Sai Gopi N

just add 3 lines in Java File and do not add code in XML and Android manifest.

只需在 Java 文件中添加 3 行,不要在 XML 和 Android 清单中添加代码。

    getSupportActionBar().setDisplayUseLogoEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher);