App icon is not visible on Actionbar in android Lo

2019-06-15 02:03发布

问题:

I just migrate from api 19 Kitkat to 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.

回答1:

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:

  • 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. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

However, if you want an application icon, setLogo() is the correct method.



回答2:

I would also like to show my awesome app icon in Lollipop+, so here's what I used.

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

Source: ActionBar#setDisplayShowHomeEnabled(boolean)



回答3:

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. Original answer - App compat actionbar v21 app icon is not showing

This code worked on my device. Let me know if you have any additional questions.



回答4:

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:



回答5:

you have to used toolbar in your app like below.

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

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

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);
        }


回答6:

Try this:-

getSupportActionBar.setDisplayHomeAsUpEnabled(true);