BottomNavigationView 不突出显示自定义图标
BottomNavigationView does not highlight custom icon
我想实现 BottomNavigationView
并已将 material.io 的一个图标作为 png 添加到我的可绘制对象中。当我将其插入为:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
<!-- This is my item added to the normal template -->
<item
android:id="@+id/navigation_more"
android:icon="@drawable/ic_more_horiz_black_24dp"
android:title="@string/title_more" />
</menu>
并在主activity中使用它,它显示出来,但在模拟器上按下时该项目不会高亮,而其他的会(高亮,我的意思是它会稍微爆炸并改变到原色,在下面显示一些文本)。我尝试了矢量和 .png,没有任何效果。我 运行 向后兼容 Android 5.0(目标版本 27)。
家 Activity 目前的样子:
public class HomeActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_more:
mTextMessage.setText(R.string.title_more);
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
文本视图设置正确,图标不会突出显示。
你的导航方法总是return false,尝试return true,当有开关处理的情况时;)
例如:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_more:
mTextMessage.setText(R.string.title_more);
return true; // this was my mistake...
default:
return false;
}
return false;
}
我想实现 BottomNavigationView
并已将 material.io 的一个图标作为 png 添加到我的可绘制对象中。当我将其插入为:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
<!-- This is my item added to the normal template -->
<item
android:id="@+id/navigation_more"
android:icon="@drawable/ic_more_horiz_black_24dp"
android:title="@string/title_more" />
</menu>
并在主activity中使用它,它显示出来,但在模拟器上按下时该项目不会高亮,而其他的会(高亮,我的意思是它会稍微爆炸并改变到原色,在下面显示一些文本)。我尝试了矢量和 .png,没有任何效果。我 运行 向后兼容 Android 5.0(目标版本 27)。
家 Activity 目前的样子:
public class HomeActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_more:
mTextMessage.setText(R.string.title_more);
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
文本视图设置正确,图标不会突出显示。
你的导航方法总是return false,尝试return true,当有开关处理的情况时;)
例如:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_more:
mTextMessage.setText(R.string.title_more);
return true; // this was my mistake...
default:
return false;
}
return false;
}