我想為我的 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()
}
}
uj5u.com熱心網友回復:
在添加抽屜之前它作業得很好。不,它沒有。
我的布局呢?
目前,您的視圖未正確附加到導致不一致行為的約束,例如跳轉到螢屏的左上角。您最好檢查約束布局

第一張截圖是手機上的截圖第二張應該是這樣的
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/476545.html
