繁体   English   中英

问题导航抽屉,Android,Kotlin

[英]Problems Navigation Drawer, Android, Kotlin

我想为我的 android 应用程序制作一个侧边菜单。 为此,在 MainActivity 中,我将根布局元素从 ConstraintLayout 更改为 DrawerLayout,添加了我自己的工具栏,然后编写了侧边菜单代码。 但是当我开始在手机上测试应用程序时,我的起始片段(fragment_start)的元素向右移动。 我坐了一整天,但不明白是什么错误,我在哪里搞砸了? 还有几个与此相关的问题:为什么汉堡包没有显示在工具栏上,尽管我为此添加了代码( appBarConfig = AppBarConfiguration(navController.graph,binding.drawerLayout) )? 如何使侧边菜单仅在我需要的片段中打开,而不是在整个应用程序中?

我的代码:

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>

主菜单

<?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>

片段开始.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

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()
    }

}

在添加抽屉之前它工作得很好 ,它没有。

我的布局呢?

目前,您的视图未正确附加到导致不一致行为的约束,例如跳转到屏幕的左上角。 您最好检查约束布局文档视频 导航视图中的android:layout_gravity="right"可能会使应用程序崩溃,请改用start 此外,您可以将约束替换为线性布局,它更简单且适合您的视图要求。

我的菜单图标在哪里?

Idk,在使用setSupportActionBar()时会发生一些奇怪的行为。 但是您可以直接设置工具栏toolbar.setupWithNavController(navController, appBarConfig)

如何控制抽屉状态?

您可以在特定屏幕上监听目标更改navController.addOnDestinationChangedListener()和锁定抽屉drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)和虎钳。

仅供参考材料抽屉指南导航组件和用户界面

这里如手机所示

应该是这样的

第一张截图是手机上的截图第二张应该是这样的

暂无
暂无

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

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