Android Toolbar navigation icon never set

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26525229/
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:42:29  来源:igfitidea点击:

Toolbar navigation icon never set

androidmaterial-designandroid-toolbarandroid-appcompatandroid-actionbar-compat

提问by grunk

I'm trying the new Toolbar component and having some trouble with the navigation icon. I want to implement a custom icon for back navigation :

I'm trying the new Toolbar component and having some trouble with the navigation icon. I want to implement a custom icon for back navigation :

In my manifest i set a parent to my activity :

In my manifest i set a parent to my activity :

<activity android:name=".CardsActivity" android:parentActivityName=".MainActivity">
    <!-- Parent activity meta-data to support API level 7+ -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>

I declare the toolbar like this :

I declare the toolbar like this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.lollitest.MainActivity" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:layout_marginBottom="10dp"
        android:background="?attr/colorPrimary" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_awesome_toolbar"
        android:text="@string/hello_world" />

</RelativeLayout>

Then in my activity i configure the Toolbar like this :

Then in my activity i configure the Toolbar like this :

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_good);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub");
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);

Which giving me : Toolbar with back button

Which giving me : Toolbar with back button

The back icon is notthe one i set with setNavigationIcon()! Whatever drawable i give to the method the navigation icon is always the back arrow.

The back icon is notthe one i set with setNavigationIcon()! Whatever drawable i give to the method the navigation icon is always the back arrow.

I have tried to remove the parent association in the manifest but the only effect is (obviously) to prevent the button to go back.

I have tried to remove the parent association in the manifest but the only effect is (obviously) to prevent the button to go back.

On contrary if i want the default back arrow icon and don't call setNavigationIcon()i don't have any icon at all.

On contrary if i want the default back arrow icon and don't call setNavigationIcon()i don't have any icon at all.

What is the correct way to handle the navigation icon in toolbar (custom and default) ?

What is the correct way to handle the navigation icon in toolbar (custom and default) ?

NOte : i'm running my test on Android 4.4

NOte : i'm running my test on Android 4.4

回答by Gabriele Mariotti

Currently you can use it, changing the order: (it seems to be a bug)

Currently you can use it, changing the order: (it seems to be a bug)

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

toolbar.setNavigationIcon(R.drawable.ic_good);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub");
toolbar.setLogo(R.drawable.ic_launcher);

回答by Raffaeu

Specific to the navigation icon, this is the correct order

Specific to the navigation icon, this is the correct order

// get the actionbar as Toolbar and set it up
Toolbar toolbar = (Toolbar) findViewById(R.id.signIn_toolbar);
setSupportActionBar(toolbar);

Inform the Toolbar to provide back navigation. This will set the icon to the default material icon

Inform the Toolbar to provide back navigation. This will set the icon to the default material icon

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Later override the icon with the custom one, in my case the Holo back icon

Later override the icon with the custom one, in my case the Holo back icon

toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_36dp);

回答by Alexey

(The answer to user802421)

(The answer to user802421)

private void setToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_action_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
}

toolbar.xml

toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolbar_height"
    android:background="?attr/colorPrimaryDark" />

回答by Nguyên Ph?m

Use setNavigationIcon to change it. don't forget create ActionBarDrawerToggle first!

Use setNavigationIcon to change it. don't forget create ActionBarDrawerToggle first!

sample code work for me:

sample code work for me:

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


    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);

    toggle.syncState();

    toolbar.setNavigationIcon(R.drawable.ic_menu);

回答by xiaohu Wang

I just found the solution. It is really very simple:

I just found the solution. It is really very simple:

mDrawerToggle.setDrawerIndicatorEnabled(false);

Hope it will help you.

Hope it will help you.

回答by Lettings Mall

I used the method below which really is a conundrum of all the ones above. I also found that onOptionsItemSelected is never activated.

I used the method below which really is a conundrum of all the ones above. I also found that onOptionsItemSelected is never activated.

    mDrawerToggle.setDrawerIndicatorEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    if (toolbar != null) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

回答by Gogi Bobina

You can use invalidate()method to change toolbar state in any place. Example:

You can use invalidate()method to change toolbar state in any place. Example:

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

toolbar.setNavigationIcon(R.mipmap.arrow_white);
toolbar.invalidate();       // restore toolbar

回答by Jakub Michalko

I had simillar problem. After a big headache I found, that my ActionBarDrawerTogglewas modifying the icon, also when it should not modify the icon (because I didn't give reference to toolbar to the toggle component). So in my NavigationDrawerFragmentclass (that handles the opening and closing) in setUp(...)method I set
mDrawerToggle.setHomeAsUpIndicator(R.drawable.app_icon);
and finally it worked.

I had simillar problem. After a big headache I found, that my ActionBarDrawerTogglewas modifying the icon, also when it should not modify the icon (because I didn't give reference to toolbar to the toggle component). So in my NavigationDrawerFragmentclass (that handles the opening and closing) in setUp(...)method I set
mDrawerToggle.setHomeAsUpIndicator(R.drawable.app_icon);
and finally it worked.

回答by Valentin Blokhin

I tried to set up toolbar like @Gabriele Mariotti, but I had some problem with title. So then I set order to

I tried to set up toolbar like @Gabriele Mariotti, but I had some problem with title. So then I set order to

toolbar.setTitle("Title")
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_good);

and it works.

and it works.

回答by Nidhin

Remove this line from activity if you have added

Remove this line from activity if you have added

 @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

Then set icon

Then set icon

 getSupportActionBar().setHomeAsUpIndicator(icon);