Material 지원 창 레시피
이 레시피에서는 Material 3 적응형 라이브러리의 SupportingPaneSceneStrategy를 사용하여 기본 창과 지원 창이 있는 적응형 레이아웃을 만드는 방법을 보여줍니다. 이 레이아웃은 큰 화면에서 기본 콘텐츠와 함께 보조 콘텐츠를 표시하는 데 유용합니다.
작동 방식
이 예시에는 MainVideo, RelatedVideos, Profile의 세 가지 대상이 있습니다.
SupportingPaneSceneStrategy
rememberSupportingPaneSceneStrategy는 이 적응형 레이아웃의 로직을 제공합니다.
-
창 역할: 각 대상에는 메타데이터를 사용하여 역할이 할당됩니다.
SupportingPaneSceneStrategy.mainPane(): 기본 콘텐츠용입니다. 이 창은 항상 표시됩니다.SupportingPaneSceneStrategy.supportingPane(): 보충 콘텐츠의 경우 이 창은 큰 화면에서 기본 창과 함께 표시됩니다.SupportingPaneSceneStrategy.extraPane(): 더 큰 화면에서 지원 창과 함께 표시할 수 있는 3차 콘텐츠에 사용됩니다.
-
적응형 레이아웃:
SupportingPaneSceneStrategy에서 레이아웃을 자동으로 처리합니다. 작은 화면에서는 기본 창만 표시됩니다. 큰 화면에서는 지원 창이 기본 창 옆에 표시됩니다. -
뒤로 탐색: 이 예시에서는
BackNavigationBehavior가PopUntilCurrentDestinationChange로 맞춤설정됩니다. 즉, 사용자가 뒤로 버튼을 누르면 지원 창이 닫히고 아래에 있는 기본 창이 표시됩니다. -
탐색: 탐색은 백 스택에서 대상을 추가하고 삭제하여 처리됩니다.
SupportingPaneSceneStrategy는 이러한 변경사항을 관찰하고 이에 따라 레이아웃을 조정합니다.
package com.example.nav3recipes.material.supportingpane /* * Copyright 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi import androidx.compose.material3.adaptive.currentWindowAdaptiveInfoV2 import androidx.compose.material3.adaptive.layout.calculatePaneScaffoldDirective import androidx.compose.material3.adaptive.navigation.BackNavigationBehavior import androidx.compose.material3.adaptive.navigation3.SupportingPaneSceneStrategy import androidx.compose.material3.adaptive.navigation3.rememberSupportingPaneSceneStrategy import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.dropUnlessResumed import androidx.navigation3.runtime.NavKey import androidx.navigation3.runtime.entryProvider import androidx.navigation3.runtime.rememberNavBackStack import androidx.navigation3.ui.NavDisplay import com.example.nav3recipes.content.ContentBlue import com.example.nav3recipes.content.ContentGreen import com.example.nav3recipes.content.ContentRed import com.example.nav3recipes.ui.setEdgeToEdgeConfig import kotlinx.serialization.Serializable @Serializable private object MainVideo : NavKey @Serializable private data object RelatedVideos : NavKey @Serializable private data object Profile : NavKey class MaterialSupportingPaneActivity : ComponentActivity() { @OptIn(ExperimentalMaterial3AdaptiveApi::class) override fun onCreate(savedInstanceState: Bundle?) { setEdgeToEdgeConfig() super.onCreate(savedInstanceState) setContent { val backStack = rememberNavBackStack(MainVideo) // Override the defaults so that there isn't a horizontal or vertical space between the panes. // See b/444438086 val windowAdaptiveInfo = currentWindowAdaptiveInfoV2() val directive = remember(windowAdaptiveInfo) { calculatePaneScaffoldDirective(windowAdaptiveInfo) .copy(horizontalPartitionSpacerSize = 0.dp, verticalPartitionSpacerSize = 0.dp) } // Override the defaults so that the supporting pane can be dismissed by pressing back. // See b/445826749 val supportingPaneStrategy = rememberSupportingPaneSceneStrategy<NavKey>( backNavigationBehavior = BackNavigationBehavior.PopUntilCurrentDestinationChange, directive = directive ) NavDisplay( backStack = backStack, onBack = { backStack.removeLastOrNull() }, sceneStrategies = listOf(supportingPaneStrategy), entryProvider = entryProvider { entry<MainVideo>( metadata = SupportingPaneSceneStrategy.mainPane() ) { ContentRed("Video content") { Button(onClick = dropUnlessResumed { backStack.add(RelatedVideos) }) { Text("View related videos") } } } entry<RelatedVideos>( metadata = SupportingPaneSceneStrategy.supportingPane() ) { ContentBlue("Related videos") { Column(horizontalAlignment = Alignment.CenterHorizontally) { Button(onClick = dropUnlessResumed { backStack.add(Profile) }) { Text("View profile") } } } } entry<Profile>( metadata = SupportingPaneSceneStrategy.extraPane() ) { ContentGreen("Profile") } } ) } } }