首页 > 解决方案 > 显示/隐藏特定片段的底部导航视图

问题描述

在我的 android 应用程序中,我希望底部菜单栏在用户聚焦 SearchView 时消失(这也会弹出软键盘)。当 SearchView 失去焦点时,我想再次显示底部导航栏。

我尝试过使用setVisibility()并且视图确实隐藏或显示,但由于某种原因它始终保持其高度。下面是我的 BottomNavigationView 的代码:

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    app:menu="@menu/bottom_navigation_menu"
    app:elevation="80dp"
    app:labelVisibilityMode="labeled"
    app:itemTextColor="@color/bottom_nav_color"
    app:itemIconTint="@color/bottom_nav_color"
    android:background="?attr/backgroundColor">

处理隐藏/显示导航栏的代码:

// Needed to close the SearchView when pressing back (instead of just losing focus)
mSearchView.setOnQueryTextFocusChangeListener(
    (v, hasFocus) -> {
        if (!hasFocus) {
            adapter.isSearchMode = false;
            bottomNavigationView.setVisibility(View.VISIBLE);
            searchMenuItem.collapseActionView();
            adapter.notifyDataSetChanged();
        } else {
            adapter.isSearchMode = true;
            bottomNavigationView.setVisibility(View.GONE);
            searchMenuItem.collapseActionView();
            adapter.notifyDataSetChanged();
        }
    });

BottomNavigationView 由 LinearLayout 持有,如下所示:

<LinearLayout
                android:id="@+id/footer"
                android:baselineAligned="false"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_alignParentBottom="true"
                android:orientation="vertical">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="3dp"
                    android:background="@drawable/bottom_bar_top_shadow"/>

                <com.google.android.material.bottomnavigation.BottomNavigationView
                    android:id="@+id/bottom_navigation"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    app:menu="@menu/bottom_navigation_menu"
                    app:elevation="80dp"
                    app:labelVisibilityMode="labeled"
                    app:itemTextColor="@color/bottom_nav_color"
                    app:itemIconTint="@color/bottom_nav_color"
                    android:background="?attr/backgroundColor">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:orientation="horizontal"
                        android:layout_marginTop="6dp"
                        android:weightSum="5"
                        android:elevation="16dp">

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center|top">

                            <TextView
                                android:id="@+id/missed_calls"
                                style="@style/unread_count_font"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@drawable/unread_message_count_bg"
                                android:layout_marginStart="20dp"
                                android:gravity="center"
                                android:visibility="gone"/>

                        </RelativeLayout>

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center|top">

                            <TextView
                                android:id="@+id/missed_chats"
                                style="@style/unread_count_font"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@drawable/unread_message_count_bg"
                                android:layout_marginStart="20dp"
                                android:gravity="center"
                                android:visibility="gone"/>

                        </RelativeLayout>

                    </LinearLayout>

                </com.google.android.material.bottomnavigation.BottomNavigationView>


            </LinearLayout>

标签: javaandroidmaterial-designvisibilitybottomnavigationview

解决方案


在您的 BottomNavigation 上使用此代码段:

setVisibility(View.GONE)

View.Gone = 没有空间可供查看。


推荐阅读