显示/隐藏特定片段的底部导航视图



在我的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();
}
});

底部导航视图由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>

在BottomNavigation上使用以下代码片段:

setVisibility(View.GONE)

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

相关内容

  • 没有找到相关文章

最新更新