Android 当我选择其他项目时,BottomNavigationView中的第一个项目保持高亮显示
导航栏中的第一项保持高亮显示 当我单击导航栏上的其他项目时,内容将更改,但相应的项目不会突出显示,只需继续突出显示第一个项目Android 当我选择其他项目时,BottomNavigationView中的第一个项目保持高亮显示,android,android-navigation,Android,Android Navigation,导航栏中的第一项保持高亮显示 当我单击导航栏上的其他项目时,内容将更改,但相应的项目不会突出显示,只需继续突出显示第一个项目 bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSel
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()){
case R.id.number:
fragment=new data();
break;
case R.id.graph:
fragment=new graph();
break;
}
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_drawer, fragment);
fragmentTransaction.commit();
return true;
}
});
这是听众
<RelativeLayout 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.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:menu="@menu/navigation" />
<LinearLayout
android:id="@+id/graphlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/navigation"
android:orientation="vertical"
android:gravity="center_vertical">
</LinearLayout>
</RelativeLayout>
这是xml
<?xml version="1.0" encoding="utf-8"?>
这是meun.xml,它包含在设计支持库中,从25.0.0版开始。您可以在build.gradle文件中包含以下行(您还需要AppCompat支持库作为设计支持库的依赖项): 首先使用腰带在项目中编译这两个腰带
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
然后创建xml布局:
<android.support.design.widget.BottomNavigationView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@color/darkGrey"
app:itemIconTint="@color/bottom_navigation_item_background_colors"
app:itemTextColor="@color/bottom_navigation_item_background_colors"
app:menu="@menu/menu_bottom_navigation" />
请尝试此代码。这通常是由于当选择导航项时方法返回false值而导致的。您可以在onNavigationItemSelected方法中使用remove all code(除去return true之外的所有代码)来检查这一点。就这样,
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return true;
}
});
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.number:
//Open fragment or do whatever you want.
return true;
case R.id.graph:
//Open another fragment or do whatever you want.
return true;
}
return false;
}
});
然后你会看到它的工作,但内容没有改变。对于内容更改,如果我是您,我会将默认返回值更改为false,如下所示
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return true;
}
});
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.number:
//Open fragment or do whatever you want.
return true;
case R.id.graph:
//Open another fragment or do whatever you want.
return true;
}
return false;
}
});
通过
menu.xml
文件R.id.content\u抽屉布局在哪里我有四个页脚图标。但对于一个选项卡,它有许多片段。通过菜单中的另一个标签单击时,如何使选项卡高亮显示。例如,当我选择配置文件时,配置文件屏幕选项卡高亮显示。然后我在菜单项中选择了主标签。它重定向到主屏幕,但配置文件图标仍显示为高亮度。当我从一个框架移动到另一个片段时,如何使其发生变化。你能澄清一下吗,这很奇怪。我正在用Kotlin编程-当我将turn设置为true,没有代码时,它可以正常工作,但你的建议却不行…这只是当导航项由外部BT鼠标选择时的问题,如果用手触摸则没有问题
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return true;
}
});
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.number:
//Open fragment or do whatever you want.
return true;
case R.id.graph:
//Open another fragment or do whatever you want.
return true;
}
return false;
}
});