Android: can't change NavigationIcon of v7 Support Toolbar

早过忘川 提交于 2021-01-29 05:07:47

问题


I'm developing an Activity using SDK v22 (I use targetSDKVersion 22 e minSdkVersion 18 for backward compatibility). My activity uses a navigationbar called by the AppBar.

I would like change the NavigationIcon over the app bar but I couldn't. I tried with "app:navigationIcon" setting in XML layout and with setNavigationIcon method.

Any idea?

Here my Activity Class code:

public class MyActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


        toolbar.setTitleTextColor(getResources().getColor(R.color.text_color));  
        toolbar.setNavigationIcon(R.drawable.icon_nav);  //doesn't work!!
        setSupportActionBar(toolbar);


        DrawerLayout 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();

        //getting nav bar object references
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

       //...
    }

here my layout xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_my_activity" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView android:id="@+id/nav_view"
        android:layout_width="wrap_content" android:layout_height="match_parent"
        app:itemIconTint="@color/black"

        android:layout_gravity="start" android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_job_list" app:menu="@menu/activity_job_list_drawer" />

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

and here there is my app bar layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context="com.lr.j2go.MyActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay" >

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            app:navigationIcon="@drawable/ic_nav_bar_menu"
            app:collapseIcon="@drawable/ic_nav_bar_menu"
            android:navigationIcon="@drawable/ic_nav_bar_menu"

            android:background="@color/white" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_job_list" />


</android.support.design.widget.CoordinatorLayout>

I tried with app:navigationIcon and android:navigationIcon but nothing to do...

Thank you in advance for any ideas and answers!!!


回答1:


Try to move your setNavigationIcon code below toggle.syncState(),like this:

    ...    
    DrawerLayout 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.icon_nav);

Reference here.



来源:https://stackoverflow.com/questions/33627483/android-cant-change-navigationicon-of-v7-support-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!