java - 显示/隐藏特定片段的底部导航视图
问题描述
在我的 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>
解决方案
在您的 BottomNavigation 上使用此代码段:
setVisibility(View.GONE)
View.Gone = 没有空间可供查看。
推荐阅读
- c# - 更改 REST API 响应以从 VSTS 工具获取测试运行结果
- jenkins - 如何使用 httprequest 插件 jenkins 发送文件(zip)
- django - django - 如何保存表单的用户信息?
- javascript - 在函数内部调用类方法
- excel - Excel vba - 如何在单元格中找到第 8 位数字并评估它是否正确
- python - 通过使用python连接其标题与列表中的字符串匹配的所有列,在数据框中生成额外的列
- python - django orm 从其他数据库获取子数据
- node.js - NodeJS - 配置无法加载自定义环境变量
- c# - 将List volume命令的结果重定向到c#中的列表框
- html - 在同一行上为徽标和文本设置样式 CSS