android 删除ListDetailPaneScaffold的填充

vmpqdwk3  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(165)

如何删除ListDetailPaneScaffold创建的填充?

  1. import android.os.Bundle
  2. import androidx.activity.ComponentActivity
  3. import androidx.activity.compose.setContent
  4. import androidx.compose.foundation.layout.Arrangement
  5. import androidx.compose.foundation.layout.Box
  6. import androidx.compose.foundation.layout.Column
  7. import androidx.compose.foundation.layout.Row
  8. import androidx.compose.foundation.layout.fillMaxHeight
  9. import androidx.compose.foundation.layout.fillMaxSize
  10. import androidx.compose.foundation.layout.fillMaxWidth
  11. import androidx.compose.foundation.layout.padding
  12. import androidx.compose.material3.ExperimentalMaterial3Api
  13. import androidx.compose.material3.MaterialTheme
  14. import androidx.compose.material3.Surface
  15. import androidx.compose.material3.Text
  16. import androidx.compose.material3.VerticalDivider
  17. import androidx.compose.material3.adaptive.AnimatedPane
  18. import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
  19. import androidx.compose.material3.adaptive.ListDetailPaneScaffold
  20. import androidx.compose.material3.adaptive.ListDetailPaneScaffoldRole
  21. import androidx.compose.material3.adaptive.rememberListDetailPaneScaffoldNavigator
  22. import androidx.compose.ui.Alignment
  23. import androidx.compose.ui.Modifier
  24. import androidx.compose.ui.unit.dp
  25. @ExperimentalMaterial3Api
  26. class MainActivity : ComponentActivity() {
  27. @OptIn(ExperimentalMaterial3AdaptiveApi::class)
  28. override fun onCreate(savedInstanceState: Bundle?) {
  29. super.onCreate(savedInstanceState)
  30. setContent {
  31. val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator()
  32. ListDetailPaneScaffold(
  33. scaffoldState = scaffoldNavigator.scaffoldState,
  34. listPane = {
  35. AnimatedPane(
  36. modifier = Modifier.fillMaxSize()
  37. ) {
  38. Surface(
  39. color = MaterialTheme.colorScheme.secondary,
  40. onClick = {
  41. scaffoldNavigator.navigateTo(ListDetailPaneScaffoldRole.Detail)
  42. }
  43. ) {
  44. Text("List")
  45. }
  46. }
  47. },
  48. extraPane = {
  49. AnimatedPane(
  50. modifier = Modifier.fillMaxSize()
  51. ) {
  52. Surface(
  53. modifier = Modifier.fillMaxSize(),
  54. color = MaterialTheme.colorScheme.tertiary,
  55. onClick = {
  56. scaffoldNavigator.navigateBack()
  57. }
  58. ) {
  59. Text("Extra")
  60. }
  61. }
  62. }
  63. ) {
  64. AnimatedPane(
  65. modifier = Modifier
  66. ) {
  67. Surface(
  68. color = MaterialTheme.colorScheme.primary,
  69. ) {
  70. Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
  71. Text("Detail")
  72. Row(
  73. modifier = Modifier
  74. .fillMaxWidth()
  75. .padding(horizontal = 4.dp),
  76. horizontalArrangement = Arrangement.spacedBy(8.dp)
  77. ) {
  78. Surface(
  79. onClick = {
  80. scaffoldNavigator.navigateBack()
  81. },
  82. modifier = Modifier
  83. .weight(0.5f)
  84. .fillMaxHeight(),
  85. color = MaterialTheme.colorScheme.primary.copy(alpha = 0.8f)
  86. ) {
  87. Box(
  88. modifier = Modifier.fillMaxSize(),
  89. contentAlignment = Alignment.Center
  90. ) {
  91. Text("Previous")
  92. }
  93. }
  94. VerticalDivider()
  95. Surface(
  96. onClick = {
  97. scaffoldNavigator.navigateTo(ListDetailPaneScaffoldRole.Extra)
  98. },
  99. modifier = Modifier
  100. .weight(0.5f)
  101. .fillMaxHeight(),
  102. color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f)
  103. ) {
  104. Box(
  105. modifier = Modifier.fillMaxSize(),
  106. contentAlignment = Alignment.Center
  107. ) {
  108. Text("Next")
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }

字符串


的数据

yb3bgrhw

yb3bgrhw1#

通过覆盖scaffoldDirective解决了这个问题:

  1. import android.os.Bundle
  2. import androidx.activity.ComponentActivity
  3. import androidx.activity.compose.setContent
  4. import androidx.compose.foundation.layout.Arrangement
  5. import androidx.compose.foundation.layout.Box
  6. import androidx.compose.foundation.layout.Column
  7. import androidx.compose.foundation.layout.PaddingValues
  8. import androidx.compose.foundation.layout.Row
  9. import androidx.compose.foundation.layout.fillMaxHeight
  10. import androidx.compose.foundation.layout.fillMaxSize
  11. import androidx.compose.foundation.layout.fillMaxWidth
  12. import androidx.compose.foundation.layout.padding
  13. import androidx.compose.material3.ExperimentalMaterial3Api
  14. import androidx.compose.material3.MaterialTheme
  15. import androidx.compose.material3.Surface
  16. import androidx.compose.material3.Text
  17. import androidx.compose.material3.VerticalDivider
  18. import androidx.compose.material3.adaptive.AnimatedPane
  19. import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
  20. import androidx.compose.material3.adaptive.HingePolicy
  21. import androidx.compose.material3.adaptive.ListDetailPaneScaffold
  22. import androidx.compose.material3.adaptive.ListDetailPaneScaffoldRole
  23. import androidx.compose.material3.adaptive.PaneScaffoldDirective
  24. import androidx.compose.material3.adaptive.Posture
  25. import androidx.compose.material3.adaptive.WindowAdaptiveInfo
  26. import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
  27. import androidx.compose.material3.adaptive.rememberListDetailPaneScaffoldNavigator
  28. import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
  29. import androidx.compose.ui.Alignment
  30. import androidx.compose.ui.Modifier
  31. import androidx.compose.ui.geometry.Rect
  32. import androidx.compose.ui.unit.Dp
  33. import androidx.compose.ui.unit.dp
  34. @OptIn(ExperimentalMaterial3AdaptiveApi::class)
  35. private fun getExcludedVerticalBounds(posture: Posture, hingePolicy: HingePolicy): List<Rect> {
  36. return when (hingePolicy) {
  37. HingePolicy.AvoidSeparating -> posture.separatingVerticalHingeBounds
  38. HingePolicy.AvoidOccluding -> posture.occludingVerticalHingeBounds
  39. HingePolicy.AlwaysAvoid -> posture.allVerticalHingeBounds
  40. else -> emptyList()
  41. }
  42. }
  43. @ExperimentalMaterial3AdaptiveApi
  44. fun calculateStandardPaneScaffoldDirective(
  45. windowAdaptiveInfo: WindowAdaptiveInfo,
  46. verticalHingePolicy: HingePolicy = HingePolicy.AvoidSeparating
  47. ): PaneScaffoldDirective {
  48. val maxHorizontalPartitions: Int
  49. val contentPadding: PaddingValues
  50. val verticalSpacerSize: Dp
  51. when (windowAdaptiveInfo.windowSizeClass.widthSizeClass) {
  52. WindowWidthSizeClass.Compact -> {
  53. maxHorizontalPartitions = 1
  54. contentPadding = PaddingValues(0.dp)
  55. verticalSpacerSize = 0.dp
  56. }
  57. WindowWidthSizeClass.Medium -> {
  58. maxHorizontalPartitions = 1
  59. contentPadding = PaddingValues(0.dp)
  60. verticalSpacerSize = 0.dp
  61. }
  62. else -> {
  63. maxHorizontalPartitions = 2
  64. contentPadding = PaddingValues(0.dp)
  65. verticalSpacerSize = 0.dp
  66. }
  67. }
  68. val maxVerticalPartitions: Int
  69. val horizontalSpacerSize: Dp
  70. if (windowAdaptiveInfo.windowPosture.isTabletop) {
  71. maxVerticalPartitions = 2
  72. horizontalSpacerSize = 0.dp
  73. } else {
  74. maxVerticalPartitions = 1
  75. horizontalSpacerSize = 0.dp
  76. }
  77. return PaneScaffoldDirective(
  78. contentPadding,
  79. maxHorizontalPartitions,
  80. verticalSpacerSize,
  81. maxVerticalPartitions,
  82. horizontalSpacerSize,
  83. getExcludedVerticalBounds(windowAdaptiveInfo.windowPosture, verticalHingePolicy)
  84. )
  85. }
  86. @ExperimentalMaterial3Api
  87. class MainActivity : ComponentActivity() {
  88. @OptIn(ExperimentalMaterial3AdaptiveApi::class)
  89. override fun onCreate(savedInstanceState: Bundle?) {
  90. super.onCreate(savedInstanceState)
  91. setContent {
  92. val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator(
  93. scaffoldDirective = calculateStandardPaneScaffoldDirective(currentWindowAdaptiveInfo()),
  94. )
  95. ListDetailPaneScaffold(
  96. scaffoldState = scaffoldNavigator.scaffoldState,
  97. listPane = {
  98. AnimatedPane(
  99. modifier = Modifier.fillMaxSize()
  100. ) {
  101. Surface(
  102. color = MaterialTheme.colorScheme.secondary,
  103. onClick = {
  104. scaffoldNavigator.navigateTo(ListDetailPaneScaffoldRole.Detail)
  105. }
  106. ) {
  107. Text("List")
  108. }
  109. }
  110. },
  111. extraPane = {
  112. AnimatedPane(
  113. modifier = Modifier.fillMaxSize()
  114. ) {
  115. Surface(
  116. modifier = Modifier.fillMaxSize(),
  117. color = MaterialTheme.colorScheme.tertiary,
  118. onClick = {
  119. scaffoldNavigator.navigateBack()
  120. }
  121. ) {
  122. Text("Extra")
  123. }
  124. }
  125. }
  126. ) {
  127. AnimatedPane(
  128. modifier = Modifier
  129. ) {
  130. Surface(
  131. color = MaterialTheme.colorScheme.primary,
  132. ) {
  133. Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
  134. Text("Detail")
  135. Row(
  136. modifier = Modifier
  137. .fillMaxWidth()
  138. .padding(horizontal = 4.dp),
  139. horizontalArrangement = Arrangement.spacedBy(8.dp)
  140. ) {
  141. Surface(
  142. onClick = {
  143. scaffoldNavigator.navigateBack()
  144. },
  145. modifier = Modifier
  146. .weight(0.5f)
  147. .fillMaxHeight(),
  148. color = MaterialTheme.colorScheme.primary.copy(alpha = 0.8f)
  149. ) {
  150. Box(
  151. modifier = Modifier.fillMaxSize(),
  152. contentAlignment = Alignment.Center
  153. ) {
  154. Text("Previous")
  155. }
  156. }
  157. VerticalDivider()
  158. Surface(
  159. onClick = {
  160. scaffoldNavigator.navigateTo(ListDetailPaneScaffoldRole.Extra)
  161. },
  162. modifier = Modifier
  163. .weight(0.5f)
  164. .fillMaxHeight(),
  165. color = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f)
  166. ) {
  167. Box(
  168. modifier = Modifier.fillMaxSize(),
  169. contentAlignment = Alignment.Center
  170. ) {
  171. Text("Next")
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }

字符串

展开查看全部

相关问题