簡體   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