在Android 7上,使用Androidx库的导航图标可能会遇到兼容性问题。下面是一个解决方法,包含代码示例:
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
com.google.android.material.bottomnavigation.BottomNavigationView作为导航栏的父布局。例如:
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
menu.findItem(R.id.menu_item_home).setIcon(R.drawable.ic_home_selected);
menu.findItem(R.id.menu_item_profile).setIcon(R.drawable.ic_profile_unselected);
其中,R.id.menu_item_home和R.id.menu_item_profile是你在菜单文件中定义的菜单项。R.drawable.ic_home_selected和R.drawable.ic_profile_unselected分别是选中和未选中状态的图标资源。
onCreateOptionsMenu方法中,将导航栏的图标显示为原始的向上箭头图标。例如:@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_up_arrow);
return true;
}
其中,R.menu.menu_main是你的菜单文件,R.drawable.ic_up_arrow是向上箭头的图标资源。
通过以上步骤,你可以解决Android 7上使用Androidx库的导航图标兼容性问题,并正确显示导航栏的图标。