java - Android:底部导航 View 菜单之间切换的问题

标签 java android android-studio android-layout bottomnavigationview

我正在为我的应用程序使用底部导航 View 。这里我在菜单中添加了 3 个项目,并自定义了底部导航 View 的 app:itemTextAppearanceActive 和 app:itemTextAppearanceInactive 属性,如下所示:

    <android.support.constraint.ConstraintLayout
        android:layout_below="@+id/tb_home"
        android:id="@+id/cl_home_stats"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bnv_home_stats"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            android:soundEffectsEnabled="true"
            app:labelVisibilityMode="labeled"
            app:itemTextAppearanceActive="@style/navTextActive"
            app:itemTextAppearanceInactive="@style/navTextInactive"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:menu="@menu/navigation"/>
    </android.support.constraint.ConstraintLayout>

在样式 xml 文件中:

<style name="navTextInactive">
    <item name="android:textSize">20dp</item>
    <item name="android:textColor">@color/colorInActive</item>
</style>

<style name="navTextActive">
    <item name="android:textSize">20dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/colorActive</item>
</style>

当点击导航菜单时,其他菜单的 Activity 和非 Activity 状态没有变化。即使您选择其他菜单,主动选择仍保留在第一个菜单中。

Activity View 的完整 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".HomeActivity"
    tools:showIn="@layout/app_bar_home">

    <RelativeLayout
        android:id="@+id/rl_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible">
        <android.support.design.widget.TabLayout
            android:id="@+id/tb_home"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextAppearance="@style/MyCustomTabText"
            android:visibility="gone">

            <android.support.design.widget.TabItem
                android:id="@+id/tb_home_billing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_billing"/>

            <android.support.design.widget.TabItem
                android:id="@+id/tb_home_stats"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_stats" />
        </android.support.design.widget.TabLayout>

        <RelativeLayout
            android:layout_below="@+id/tb_home"
            android:id="@+id/rl_home_billing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">
            <Button
                android:id="@+id/bt_home_billing_order"
                android:layout_alignParentStart="true"
                android:layout_width="160dp"
                android:layout_height="80dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:text="@string/title_order"
                android:textSize="20sp"
                android:textStyle="bold"/>
            <Button
                android:id="@+id/bt_home_billing_parcel"
                android:layout_alignParentEnd="true"
                android:layout_width="160dp"
                android:layout_height="80dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                android:text="@string/title_parcel"
                android:textSize="20sp"
                android:textStyle="bold"/>

            <ExpandableListView
                android:id="@+id/elv_home_billing_unsettled"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/bt_home_billing_order"
                android:layout_marginTop="30dp"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
                android:divider="@android:color/darker_gray"
                android:dividerHeight="0.5dp">
            </ExpandableListView>
        </RelativeLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_below="@+id/tb_home"
            android:id="@+id/cl_home_stats"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone">
            <android.support.design.widget.BottomNavigationView
                android:id="@+id/bnv_home_stats"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="?android:attr/windowBackground"
                android:soundEffectsEnabled="true"
                app:labelVisibilityMode="labeled"
                app:itemTextAppearanceActive="@style/navTextActive"
                app:itemTextAppearanceInactive="@style/navTextInactive"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:menu="@menu/navigation"/>
        </android.support.constraint.ConstraintLayout>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

最佳答案

首先在主布局中添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        tools:ignore="MissingConstraints" >

    </com.google.android.material.tabs.TabLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

在名为 bottom_nav_menu.xml 的菜单中:

<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: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" />

你想切换哪个 fragment ,你应该添加那些 fragment

Activity 代码是:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

并且从 Android-Studio 您可以自动启动它:

新项目>手机和平板电脑>抽屉导航 Activity >完成

或者对于新的 Activity :

新建 > Activity > 底部导航 Activity > 完成。

希望对你有帮助,或者可以在评论里问我。 谢谢。

关于java - Android:底部导航 View 菜单之间切换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60023946/

相关文章:

android - 如何在 Android 中设置 Realm

java - 在 Java 中将 Wikitext 转换为纯文本

android - 导入 Android APK 存档?

java - 我可以通过编程方式将 TextView 的 xml 文本与另一个字符串进行比较吗?

java - 我不明白为什么这个应用程序会崩溃

android-studio - 任务 ':tweetLanes:dexDebug' 执行失败

java - 在 Spring Boot 应用程序中禁用 Spring Security

java - 关于计算不同键的总数

java - java中interrupt()方法与线程状态的精确行为

android-studio - 错误 : Can't determine type for tag '<item name="APP_URL"type ="String" >"https://www.dummyurl_dev.com/"</item>' in build gradleResValues. xml