如何隐藏底部导航栏?

时间:2020-05-09 06:17:03

标签: java android

我已经在android中创建了一个底部导航栏,现在我想在向上滚动时将其隐藏,而在向下滚动时将其显示。有人可以让我知道怎么做吗?我尚未使用任何库创建导航栏。我用线性和相对布局制作的。 这是我的MainActivity.java XML文件。

<android.support.v4.widget.DrawerLayout
    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/MageNative_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:fitsSystemWindows="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:id="@+id/MageNative_toolbar"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            app:theme="@style/AppTheme">
            <ImageView
                android:id="@+id/toolimage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:contentDescription="@string/app_name"
                />
            <TextView
                android:id="@+id/tooltext"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:gravity="center_vertical"
                android:text="@string/app_name"
                android:textAllCaps="true"
                android:textColor="@color/black"
                android:textSize="15sp"
                android:visibility="gone"
                android:textStyle="bold"
                />
        </android.support.v7.widget.Toolbar>
        <FrameLayout
            android:id="@+id/MageNative_frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:layout_marginBottom="60dp"
            android:layout_below="@+id/MageNative_toolbar">
        </FrameLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/bottom_toolbar"
            android:background="@color/white"
            android:layout_alignParentBottom="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_centerInParent="true"
                android:weightSum="1"
                android:baselineAligned="false"
                android:background="@color/white"
                android:layout_marginTop="10dp"
                >

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="0.2"
                    android:id="@+id/home_icon_sect"
                    android:orientation="vertical"
                    android:layout_height="50dp">

                    <ImageView
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="10dp"
                        android:tint="#343D4E"
                        android:src="@drawable/home"
                        android:id="@+id/home_icon"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:gravity="center"
                        android:textSize="11sp"
                        android:text="@string/home"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="0.2"
                    android:orientation="vertical"
                    android:id="@+id/categories"
                    android:layout_height="50dp">


                    <ImageView
                        android:id="@+id/wish_icon"
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/home"
                        android:tint="#343D4E" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:gravity="center"
                        android:textSize="11sp"
                        android:text="@string/categories"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="0.2"
                    android:orientation="vertical"
                    android:id="@+id/notification_icon_sect"
                    android:layout_height="50dp">

                    <ImageView
                        android:id="@+id/notification_icon"
                        android:layout_width="wrap_content"
                        android:layout_height="25dp"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/search"
                        android:tint="#343D4E" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/search"
                        android:textColor="@color/black"
                        android:textSize="11sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="0.2"
                    android:orientation="vertical"
                    android:id="@+id/user_icon_sect"
                    android:layout_height="50dp">

                    <ImageView
                        android:id="@+id/user_icon"
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="10dp"
                        android:src="@drawable/home"
                        android:tint="#343D4E" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:gravity="center"
                        android:textSize="11sp"
                        android:text="@string/account"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="0.2"
                    android:id="@+id/drawer_menu_sect"
                    android:orientation="vertical"
                    android:layout_height="50dp">




                    <ImageView
                        android:id="@+id/drawer_menu"
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_marginTop="10dp"
                        android:layout_centerHorizontal="true"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/bag"
                        android:tint="#343D4E" />



                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:gravity="center"
                        android:layout_below="@+id/drawer_menu"
                        android:textSize="11sp"
                        android:text="@string/cart"/>

                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>
    <fragment
        android:id="@+id/MageNative_fragment_navigation_drawer"
        android:name="com.freshgrocy.app.maincontainer.FragmentDrawer"
        android:layout_width="290dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:layout="@layout/fragment_drawer"
        tools:layout="@layout/fragment_drawer" />
</android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:0)

只需在您的app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"中添加BottomNavigationView

 <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       ....
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" />

答案 1 :(得分:0)

我知道可能已经晚了,但是我得到了答案,两个月前我正在搜索并且什么也没找到,但是几分钟前我找到了解决方案并检查了它在Android上是否运行良好Kitkat超越了,决定分享它。我从AppIntro库中提取了它,然后开始:

  public void setImmersiveMode(boolean isEnabled, boolean isSticky) {

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if(!isEnabled && isImmersiveModeEnabled) {
                getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

                isImmersiveModeEnabled = false;
            } else if(isEnabled) {

                int flags =  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;

                if(isSticky) {
                    flags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
                    isImmersiveModeSticky = true;
                } else {
                    flags |= View.SYSTEM_UI_FLAG_IMMERSIVE;
                    isImmersiveModeSticky = false;
                }

                getWindow().getDecorView().setSystemUiVisibility(flags);

                isImmersiveModeEnabled = true;
            }
        }
    }

您的活动中:

public boolean isImmersiveModeEnabled = false;
public boolean isImmersiveModeSticky = false;

并在您的代码中使用它:

setImmersiveMode(true,false);

setImmersiveMode(true,false);

快乐编码:)