问题描述
我正在使用SDK v22开发活动(为了向后兼容,我使用targetSDKVersion 22 e minSdkVersion 18).我的活动使用AppBar调用的导航栏.
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.
我想在应用程序栏上更改NavigationIcon,但不能.我尝试使用XML布局中的"app:navigationIcon"设置以及setNavigationIcon方法.
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.
有什么主意吗?
这里是我的活动班级代码:
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);
//...
}
这里是我的布局xml:
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>
我尝试使用app:navigationIcon和android:navigationIcon,但无事可做...
I tried with app:navigationIcon and android:navigationIcon but nothing to do...
在此先感谢您的想法和答案!
Thank you in advance for any ideas and answers!!!
推荐答案
尝试将您的 setNavigationIcon
代码移动到 toggle.syncState()
下,如下所示:
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);
参考此处.
这篇关于Android:无法更改v7支持工具栏的NavigationIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!