탐색 창에서 탐색 구성 요소를 어떻게 설정합니까? 내 앱에서 어떻게 사용합니까?
하나의 활동으로 모든 것을 수행 할 수 있습니까?
동적 도구 모음 가시성을 가진 하나의 활동과 조각으로 도구 모음 가시성을 어떻게 처리합니까? 또한 서랍을 닫고 접근 할 수 없게 만드는 데 필요한 파편이 있습니다.
이 질문은 자체 답변 질문이며 실제 QA보다 튜토리얼로 더 많이 작동합니다.
탐색 창에서 탐색 구성 요소를 어떻게 설정합니까?
내 앱에서 어떻게 사용합니까?
탐색 창 설정은 탐색 구성 요소와 관련하여 약간 다릅니다.
서랍 내비게이션이있는 새 앱을 만드는 경우 현재 자습서가 필요하지 않습니다. 하지만 여기서 이상하게 보일 수있는 몇 가지 사항과 앱의 후반부에서 서랍을 추가하기로 결정한 경우 설명하겠습니다.
첫째, 당신은 설정 당신의 필요 activity_main.xml
및 MainActivity
탐색 구조에 대한 준비가되어 :
<?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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
app_bar_main
그냥 어디에 :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
그리고 content_main
파편이 보관되는 곳입니다.
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
알아야 할 사항 : 액티비티에 AppBar가 설정되어 있지 않아야합니다 AndroidManifest.xml
.
android:theme="@style/AppTheme.NoActionBar"
당신이 통지하는 경우 app:menu="@menu/activity_main_drawer"
에 NavigationView
태그, 조각 이름은이 안에있는 것을 동일해야합니다 mobile_navigation.xml
:
<group android:checkableBehavior="single">
<item
android:id="@+id/homeFragment"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item
android:id="@+id/galleryFragment"
android:icon="@drawable/ic_menu_gallery"
android:title="@string/menu_gallery" />
<item
android:id="@+id/slideshowFragment"
android:icon="@drawable/ic_menu_slideshow"
android:title="@string/menu_slideshow" />
<item
android:id="@+id/toolsFragment"
android:icon="@drawable/ic_menu_manage"
android:title="@string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/shareFragment"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_share" />
<item
android:id="@+id/sendFragment"
android:icon="@drawable/ic_menu_send"
android:title="@string/menu_send" />
</menu>
</item>
</menu>
이렇게하면 아래에서 설명 onCreateOptionsMenu
할 내용으로 클릭을 감지 하기 위해 호출 할 필요가 없습니다 . Android 팀은 이미이를 해결했습니다. 아래를 따르십시오.
지금까지 이것은 우리가 실제로 설정하는 일반적인 서랍과 크게 다르지 않습니다. 그러나 앱의 논리 부분에서 수행해야 할 몇 가지 구성이 있습니다. 그럼 MainActivity.kt
. 먼저 다음이 필요합니다.
private var appBarConfiguration: AppBarConfiguration? = null
private var drawerLayout: DrawerLayout? = null
private var toolbar: Toolbar? = null
private var navController: NavController? = null
그 후 귀하의 onCreate
방법에서 :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar) //set the toolbar
drawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homeFragment,
R.id.galleryFragment,
R.id.slideShowFragment,
R.id.toolsFragment,
R.id.shareFragment,
R.id.sendFragment,
R.id.loginFragment,
R.id.phoneConfirmationFragment
), drawerLayout
)
setupActionBarWithNavController(navController!!, appBarConfiguration!!) //the most important part
navView.setupWithNavController(navController!!) //the second most important part
//other things unrelated
}
여기서 무슨 일이 일어나는지 보자 :
먼저에 대한 참조가 필요합니다 navController
. 는 AppBarConfiguration
단지 최상위 대상으로 열 수하려고하는 조각을 유지하는 클래스입니다. 이는 이후에 열릴 조각이 조각 백 스택에서 현재 조각을 해제한다는 것을 의미합니다. AppBarConfiguration
드로어도 있음 을에 알리는 것이 중요합니다 (생성자에서 매개 변수로 전달됨).
아래에 다음과 같은 메서드가 있습니다 onSupportNavigateUp()
.
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration!!) || super.onSupportNavigateUp()
}
이 방법은 뒤로 가기 버튼과 관련이 있습니다. 그러나 서랍 내비게이션이 있다면 너무 많이 필요하지 않을 것입니다. 이것은 백 스택에 많은 조각 (또는 적어도 두 개)이 추가 될 때 정말 유용합니다.
하나의 활동으로 모든 것을 수행 할 수 있습니까?
네, 물론입니다! 그러나 여전히 조건부 탐색에 관해서는 조금 더 많은 작업이 필요합니다. 서랍 앱의 일부가 아닌 조각을 표시하려는 경우와 같습니다. 그러나 여전히 구글은 그것으로 엄청난 진전을 이루었습니다. 여기 에서 조건부 탐색을 참조 할 수 있습니다 .
동적 도구 모음 가시성을 가진 하나의 활동과 조각으로 도구 모음 가시성을 어떻게 처리합니까? 또한 서랍을 닫고 접근 할 수 없게 만드는 데 필요한 파편이 있습니다.
다음 addOnDestinationChangedListener
에서 사용할 수 있습니다 navController
.
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.loginFragment, R.id.registerFragment, R.id.phoneConfirmationFragment -> {
toolbar?.visibility = View.GONE
drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
else -> {
toolbar?.visibility = View.VISIBLE
drawerLayout?.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
}
}
}
이제 앱에 서랍 및 탐색 구성 요소가 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다