Android:无法更改v7支持工具栏的NavigationIcon

时间:2015-11-10 10:08:10

标签: android layout navigationbar

我正在使用SDK v22开发一个Activity(为了向后兼容,我使用了targetSDKVersion 22 e minSdkVersion 18)。 我的活动使用AppBar调用的导航栏。

我想在应用栏上更改NavigationIcon,但我无法做到。我试过" app:navigationIcon"使用setNavigationIcon方法设置XML布局。

有什么想法吗?

这是我的活动类代码:

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

       //...
    }

这里是我的布局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>

这里有我的应用栏布局:

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

我尝试使用app:navigationIcon和android:navigationIcon但无事可做......

提前感谢您的任何想法和答案!

1 个答案:

答案 0 :(得分:2)

尝试将setNavigationIcon代码移到toggle.syncState()下方,如下所示:

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

参考here