简体   繁体   English

在Android Lollipop及更高版本的Actionbar上看不到应用程序图标

[英]App icon is not visible on Actionbar in android Lollipop and above

I just migrate from api 19 Kitkat to api 21 Lollipop. 我只是从api 19 Kitkat迁移到api 21 Lollipop。 And now I have found that the app icon is not there on actionbar. 现在我发现应用程序图标不在操作栏上。 I feel that my app looks kind of different. 我觉得我的应用看起来有点不同。 So is there any way to show the app icon. 那么有没有办法显示应用程序图标。

In the Material theme (and AppCompat version 21 which is based on it), the Action Bar follows the material design guidelines and uses a Toolbar : 在Material主题(以及基于它的AppCompat版本21)中,Action Bar遵循材料设计指南并使用工具栏

  • 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. 在现代Android UI中,开发人员应该更多地依赖于工具栏的视觉上不同的颜色方案而不是应用程序图标。 The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer. API 21设备和更新设备不鼓励使用应用程序图标和标题作为标准布局。

However, if you want an application icon, setLogo() is the correct method. 但是,如果您需要应用程序图标,则setLogo()是正确的方法。

I would also like to show my awesome app icon in Lollipop+, so here's what I used. 我还想在Lollipop +中展示我真棒的应用程序图标,所以这就是我使用的内容。

mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setIcon(Drawable); // Or drawable resource id.

Source: ActionBar#setDisplayShowHomeEnabled(boolean) 来源: ActionBar #setDisplayShowHomeEnabled(boolean)

This worked for me - 这对我有用 -

getSupportActionBar().setTitle("Title");
getSupportActionBar().setSubtitle("Subtitle");
getSupportActionBar().setHomeButtonEnabled(true); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | 
ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_USE_LOGO); // Customize if you need to

getSupportActionBar().setIcon(R.drawable.iconhere); // or setLogo

It is important that you setDisplayOptions as setIcon/setLogo doesn't seem to be working without it. 设置setDisplayOptions非常重要,因为没有它,setIcon / setLogo似乎不起作用。 Original answer - App compat actionbar v21 app icon is not showing 原始答案 - App compat actionbar v21 app图标未显示

This code worked on my device. 此代码适用于我的设备。 Let me know if you have any additional questions. 如果您有任何其他问题,请与我们联系。

This is how I did it: 这就是我做的方式:

package com.your.package;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

Which will give you this: 哪个会给你这个:

在此输入图像描述

you have to used toolbar in your app like below. 你必须在你的应用程序中使用工具栏,如下所示。

styles.xml styles.xml

<!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

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

v21\\styles.xml V21 \\ styles.xml

<resources>

    <style name="AppTheme" parent="AppTheme.Base">

        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>

        <item name="android:textColorPrimary">@color/textColorPrimary</item>
        <item name="android:windowBackground">@color/windowBackground</item>
        <item name="android:navigationBarColor">@color/navigationBarColor</item>

    </style>

</resources>

in your layout use toolbar like this: 在你的布局中使用这样的工具栏:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/windowBackground">

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

        </android.support.v7.widget.Toolbar>



        <FrameLayout
            android:id="@+id/flContent"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar">


        </FrameLayout>


    </RelativeLayout>



</android.support.v4.widget.DrawerLayout>

in your Java file write below code for set icon: 在您的Java文件中写下设置图标的代码:

toolbar = (Toolbar) findViewById(R.id.toolbar);


        if (toolbar != null) {

            setSupportActionBar(toolbar);

            toolbar.setNavigationIcon(R.drawable.icn_menu);
            //toolbar.setTitle("Title");
            setTitle("Title");
            //toolbar.setSubtitle("Subtitle");
            //toolbar.setLogo(R.drawable.ic_launcher);
        }

试试这个:-

getSupportActionBar.setDisplayHomeAsUpEnabled(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM