如何在主屏幕上隐藏工具栏?

Maaz Hasnain

我正在构建一个需要登录的android应用程序。我使用的是工具栏而不是操作栏,并且还安装了带有底部导航的导航抽屉。我成功隐藏了主屏幕的底部导航,但是从工具栏访问设置的菜单仍然是菜单,可能会导致用户无需登录即可访问应用程序。我想从菜单中删除设置菜单项。主页屏幕(登录屏幕)登录屏幕如下所示

MainActivity.KT:

package com.example.integratedmodulateoroperationroom

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.*
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var navController: NavController
    private lateinit var appBarConfiguration: AppBarConfiguration
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.findNavController()

        appBarConfiguration = AppBarConfiguration(
            setOf(R.id.homeFragment, R.id.loginFragment),
            drawer_layout
        )

        setSupportActionBar(toolbar)
        setupActionBarWithNavController(navController, appBarConfiguration)

        bottom_nav.setupWithNavController(navController)
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)

        navController.addOnDestinationChangedListener { _, destination, _ ->
            bottomNavigationView.visibility = if(destination.id == R.id.loginFragment) {
                View.GONE
            } else {
                View.VISIBLE
            }
        }
        nav_view.setupWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.options_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}

activity_main.XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/design_default_color_primary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_gravity="start"
        app:menu="@menu/drawer_nav_menu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>

fragment_login.XML(主屏幕):

<androidx.core.widget.NestedScrollView
    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:layout_height="match_parent"
    android:layout_width="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginFragment">

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/login_logo"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="center"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:src="@drawable/logo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/Username"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:hint="Login ID"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_logo">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </com.google.android.material.textfield.TextInputEditText>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/Password"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:hint="Password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Username"
        app:passwordToggleEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword">

        </com.google.android.material.textfield.TextInputEditText>

    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginStart="150dp"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="24dp"
        android:background="#00000000"
        android:text="Forgot Password?"
        android:textColor="@color/material_on_surface_emphasis_high_type"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Password" />

    <Button
        android:id="@+id/button_goToHomeScreen"
        android:layout_width="270dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp"
        android:background="@drawable/button_design"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text="Login"
        android:textAllCaps="false"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />


</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Maaz Hasnain

这里使用的导航控制器让我们使用addOnDestinationChangedListener因此,在这里使用它可以成功从主屏幕中删除工具栏。

            toolbar.visibility = if (destination.id == R.id.loginFragment) {
                View.GONE
            } else {
                View.VISIBLE
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在屏幕上固定ckeditor工具栏?

来自分类Dev

如何在iPad上隐藏quicktype键盘工具栏?

来自分类Dev

如何在工具栏上隐藏NavigationIcon

来自分类Dev

如何在工具栏上隐藏操作项

来自分类Dev

如何在Firefox主工具栏上添加书签按钮?

来自分类Dev

打开某些片段时如何在工具栏上隐藏/显示图标

来自分类Dev

如何使工具栏隐藏在滚动Android Studio上

来自分类Dev

如何在屏幕底部添加工具栏?

来自分类Dev

如何在屏幕底部添加工具栏?

来自分类Dev

如何在屏幕右侧创建垂直工具栏?

来自分类Dev

如何在Android中隐藏工具栏的阴影?

来自分类Dev

如何在开始目标中隐藏工具栏

来自分类Dev

如何在TeamViewer中以全屏模式隐藏工具栏?

来自分类Dev

如何在Firefox 29中隐藏“导航工具栏”?

来自分类Dev

如何在UIVideoEditorController的工具栏上添加/修改按钮?

来自分类Dev

如何在Android工具栏上使用VectorDrawable?

来自分类Dev

如何在工具栏上显示项目图标Android

来自分类Dev

如何在FragmentActivity上设置工具栏?

来自分类Dev

如何在ADOBE XD上显示工具栏

来自分类Dev

如何在Nautilus工具栏上添加/删除按钮?

来自分类Dev

如何在jqgrid工具栏上添加刷新按钮?

来自分类Dev

如何在v7工具栏上使用工具栏的主页按钮提供向上导航

来自分类Dev

如何在iOS主屏幕上显示这样的栏?

来自分类Dev

如何对齐工具栏上的按钮?

来自分类Dev

如何在工具栏按钮上隐藏文本(带有TBSTYLE_LIST的TBSTYLE_EX_MIXEDBUTTONS不会隐藏文本)

来自分类Dev

CoordinatorLayout并隐藏工具栏

来自分类Dev

隐藏/显示工具栏

来自分类Dev

如何在Webview中向下滚动时隐藏ActionBar /工具栏

来自分类Dev

如何在某些片段中隐藏工具栏或以全屏模式打开某些片段

Related 相关文章

  1. 1

    如何在屏幕上固定ckeditor工具栏?

  2. 2

    如何在iPad上隐藏quicktype键盘工具栏?

  3. 3

    如何在工具栏上隐藏NavigationIcon

  4. 4

    如何在工具栏上隐藏操作项

  5. 5

    如何在Firefox主工具栏上添加书签按钮?

  6. 6

    打开某些片段时如何在工具栏上隐藏/显示图标

  7. 7

    如何使工具栏隐藏在滚动Android Studio上

  8. 8

    如何在屏幕底部添加工具栏?

  9. 9

    如何在屏幕底部添加工具栏?

  10. 10

    如何在屏幕右侧创建垂直工具栏?

  11. 11

    如何在Android中隐藏工具栏的阴影?

  12. 12

    如何在开始目标中隐藏工具栏

  13. 13

    如何在TeamViewer中以全屏模式隐藏工具栏?

  14. 14

    如何在Firefox 29中隐藏“导航工具栏”?

  15. 15

    如何在UIVideoEditorController的工具栏上添加/修改按钮?

  16. 16

    如何在Android工具栏上使用VectorDrawable?

  17. 17

    如何在工具栏上显示项目图标Android

  18. 18

    如何在FragmentActivity上设置工具栏?

  19. 19

    如何在ADOBE XD上显示工具栏

  20. 20

    如何在Nautilus工具栏上添加/删除按钮?

  21. 21

    如何在jqgrid工具栏上添加刷新按钮?

  22. 22

    如何在v7工具栏上使用工具栏的主页按钮提供向上导航

  23. 23

    如何在iOS主屏幕上显示这样的栏?

  24. 24

    如何对齐工具栏上的按钮?

  25. 25

    如何在工具栏按钮上隐藏文本(带有TBSTYLE_LIST的TBSTYLE_EX_MIXEDBUTTONS不会隐藏文本)

  26. 26

    CoordinatorLayout并隐藏工具栏

  27. 27

    隐藏/显示工具栏

  28. 28

    如何在Webview中向下滚动时隐藏ActionBar /工具栏

  29. 29

    如何在某些片段中隐藏工具栏或以全屏模式打开某些片段

热门标签

归档