英文:
Show/Hide BottomNavigationView for specific fragment
问题
在我的 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">
处理导航栏隐藏/显示的代码如下:
// 在按下返回键时需要关闭 SearchView(而不仅仅是失去焦点)
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>
英文:
In my android application, I want the bottom menu bar to disappear when the user focuses the SearchView (this also pops the soft keyboard up). When the SearchView loses focus, I want to show the bottom navigation bar again.
I have tried using setVisibility()
and the view does hide or show, but it always retains its height for some reason.Below is the code for my 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">
Code that handles hiding/showing of the navigation bar:
// 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();
}
});
The BottomNavigationView is held by a LinearLayout like so:
<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>
答案1
得分: 0
在您的BottomNavigation中使用以下代码片段:
setVisibility(View.GONE)
View.GONE = 视图不再占用空间。
英文:
On your BottomNavigation use this code snippet:
setVisibility(View.GONE)
View.Gone = No Space lefts for view.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论