fel*_*340 5 android android-navigation
导航栏中的第一项保持突出显示。
当我点击导航栏上的其他项目时,内容会发生变化但相应的项目不会高亮显示,只是继续高亮第一个项目。
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;
}
});
Run Code Online (Sandbox Code Playgroud)
这是听众
<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>
Run Code Online (Sandbox Code Playgroud)
这是 xml
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<item
android:id="@+id/number"
android:title="number"
android:checkable="true"/>
<item
android:id="@+id/graph"
android:title="graph"
android:checkable="true"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
这是 meun.xml
这通常是因为onNavigationItemSelected 方法返回false值而发生的。您可以通过删除 onNavigationItemSelected方法中除return true之外的所有代码来检查这一点。像这样;
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
然后你会看到它有效,但内容没有改变。对于内容的更改,如果我是你,我会将默认返回值更改为 false,如下所示;
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;
}
});
Run Code Online (Sandbox Code Playgroud)
从版本 25.0.0 开始,它包含在设计支持库中。您可以使用以下行将其包含在 build.gradle 文件中(您还需要 AppCompat 支持库作为设计支持库的依赖项):
首先使用 girdle 编译项目中的 girdle
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
Run Code Online (Sandbox Code Playgroud)
然后创建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" />
Run Code Online (Sandbox Code Playgroud)
创建一个资源文件,就像导航抽屉或溢出菜单一样:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_one"
android:icon="@android:drawable/ic_dialog_map"
android:title="One"/>
<item
android:id="@+id/action_two"
android:icon="@android:drawable/ic_dialog_info"
android:title="Two"/>
<item
android:id="@+id/action_three"
android:icon="@android:drawable/ic_dialog_email"
android:title="Three"/>
<item
android:id="@+id/action_four"
android:icon="@android:drawable/ic_popup_reminder"
android:title="Four"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
显示下面的图像:
要为所选项目实现不同的着色,您应该将颜色列表资源指定为 itemBackground 和 itemTextColor,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="@color/colorAccent"
android:state_checked="false"/>
<item
android:color="@android:color/white"
android:state_checked="true"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
最后,您可以通过 setOnNavigationItemSelectedListener() 方法添加 BottomNavigation.OnNavigationItemSelectedListener 来监听选项卡选择事件:
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.action_one:
// Switch to page one
break;
case R.id.action_two:
// Switch to page two
break;
case R.id.action_three:
// Switch to page three
break;
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
试试这个代码。
| 归档时间: |
|
| 查看次数: |
3267 次 |
| 最近记录: |