简体   繁体   English

问题导航抽屉,Android,Kotlin

[英]Problems Navigation Drawer, Android, Kotlin

I wanted to make a side menu for my android application.我想为我的 android 应用程序制作一个侧边菜单。 To do this, in MainActivity, I changed the root layout element from ConstraintLayout to DrawerLayout, added my own toolbar, and then wrote the side menu code.为此,在 MainActivity 中,我将根布局元素从 ConstraintLayout 更改为 DrawerLayout,添加了我自己的工具栏,然后编写了侧边菜单代码。 But when I started testing the app on my phone, the elements of my start fragment(fragment_start) moved to the right.但是当我开始在手机上测试应用程序时,我的起始片段(fragment_start)的元素向右移动。 I sat all day, but did not understand what was the mistake, where did I screw up?我坐了一整天,但不明白是什么错误,我在哪里搞砸了? And there are a couple of questions in connection with this: Why is the hamburger not displayed on the toolbar, although I added the code for this ( appBarConfig = AppBarConfiguration(navController.graph,binding.drawerLayout) )?还有几个与此相关的问题:为什么汉堡包没有显示在工具栏上,尽管我为此添加了代码( appBarConfig = AppBarConfiguration(navController.graph,binding.drawerLayout) )? How can I make the side menu open only in the fragment that I need, and not in the whole application?如何使侧边菜单仅在我需要的片段中打开,而不是在整个应用程序中?

My code:我的代码:

activyty_main.xml active_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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/drawer_layout"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_centerVertical="true"

        >

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF3700B3"
            android:minHeight="?attr/actionBarSize"
            app:title="Отель"/>

        <fragment
            android:id="@+id/fragmentContainerView"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation" />
    </LinearLayout>

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


</androidx.drawerlayout.widget.DrawerLayout>

main_menu主菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:icon="@drawable/ic_menu"
        android:id="@+id/fragment_registration"
        android:title="Settings" />
    <item
        android:id="@+id/fragment_admin"
        android:title="About" />
</menu>

fragment_start.xml片段开始.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".start.StartFragment"
   >

    <EditText
        android:id="@+id/editInputEmail"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:layout_marginStart="80dp"
        android:background="@drawable/edit_text_style"
        android:ems="10"
        android:gravity="center"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="SpeakableTextPresentCheck" />

    <EditText
        android:id="@+id/editInputPassword"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="80dp"
        android:layout_marginTop="300dp"
        android:background="@drawable/edit_text_style"
        android:ems="10"
        android:gravity="center"
        android:inputType="textPassword"
        android:minHeight="48dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="SpeakableTextPresentCheck" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="Email"
        android:textColor="@color/black"
        android:textSize="30dp"
        app:layout_constraintEnd_toEndOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginBottom="12dp"
        android:text="Password"
        android:textColor="@color/black"
        android:textSize="30dp"
        app:layout_constraintBottom_toTopOf="@+id/editInputPassword"
        app:layout_constraintStart_toStartOf="@+id/editInputPassword" />

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginBottom="30dp"
        android:backgroundTint="#FFFFFFFF"
        android:text="Log In"
        android:textColor="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/button_registr"
        app:layout_constraintStart_toStartOf="@+id/button_registr"
        app:strokeColor="#5584E6"
        app:strokeWidth="3dp" />

    <Button
        android:id="@+id/button_registr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="140dp"
        android:layout_marginTop="100dp"
        android:backgroundTint="#FFFFFFFF"
        android:text="Registration"
        android:textColor="@color/black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editInputPassword"
        app:strokeColor="#5584E6"
        app:strokeWidth="3dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt MainActivity.kt

package com.example.hotel2


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    lateinit var appBarConfig: AppBarConfiguration



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        APP_ACTIVITY = this


        setSupportActionBar(androidx.appcompat.widget.Toolbar(this))
        val navController = findNavController(R.id.fragmentContainerView)
        appBarConfig = AppBarConfiguration(navController.graph,binding.drawerLayout)
        setupActionBarWithNavController(navController, appBarConfig)
        binding.navView.setupWithNavController(navController)


    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.fragmentContainerView)
        return navController.navigateUp(appBarConfig) || super.onSupportNavigateUp()
    }

}

It's worked perfect before adding drawer .在添加抽屉之前它工作得很好 Nope , it doesn't.,它没有。

What with my layout?我的布局呢?

Currently, your views are not properly attached to constraints that lead to inconsistence behavior, such as jumping to the top left corner of the screen.目前,您的视图未正确附加到导致不一致行为的约束,例如跳转到屏幕的左上角。 You better check constraint layout docs or videos .您最好检查约束布局文档视频 android:layout_gravity="right" in navigation view may crash the app, use start instead.导航视图中的android:layout_gravity="right"可能会使应用程序崩溃,请改用start Also, you can replace constraint with linear layout , it simpler and fits to your view requirements.此外,您可以将约束替换为线性布局,它更简单且适合您的视图要求。

Where is my menu icon?我的菜单图标在哪里?

Idk, some strange behavior occurs while working with setSupportActionBar() . Idk,在使用setSupportActionBar()时会发生一些奇怪的行为。 But you can setup toolbar directly toolbar.setupWithNavController(navController, appBarConfig) .但是您可以直接设置工具栏toolbar.setupWithNavController(navController, appBarConfig)

How to control drawer state?如何控制抽屉状态?

You can listen destination changes navController.addOnDestinationChangedListener() and lock drawer in particular screens drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) and vise-verse.您可以在特定屏幕上监听目标更改navController.addOnDestinationChangedListener()和锁定抽屉drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)和虎钳。

FYI material drawer guideline , nav components & ui .仅供参考材料抽屉指南导航组件和用户界面

这里如手机所示

应该是这样的

The first screenshot is as shown on the phone The second is how it should be第一张截图是手机上的截图第二张应该是这样的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM