我正在使用SDK v22开发活动(我使用targetSDKVersion 22 e minSdkVersion 18来实现向后兼容性)。
我的活动使用AppBar调用的导航栏。
我想在应用程序栏上更改NavigationIcon,但不能。我尝试使用XML布局中的“ app:navigationIcon”设置以及setNavigationIcon方法。
任何想法?
这里是我的活动类代码:
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,但无事可做...
预先感谢您的任何想法和答案!!!
最佳答案
尝试将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。
关于android - Android:无法更改v7支持工具栏的NavigationIcon,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33627483/