我们很难在 Android 上顺利调整此处 SDK map 的大小。
我们希望平滑地将 map 调整到底部工作表的collapse
和hidden
状态,如 所示。
但如您所见,它并没有真正调整大小,而是跳转到新位置,同时 map 保持其尺寸并且不缩放。
这就是我们所做的:
...
<com.here.sdk.mapview.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/nine_grid_unit" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/menuBottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
android:elevation="@dimen/four_grid_unit"
android:focusable="true"
app:behavior_hideable="true"
app:behavior_peekHeight="@dimen/thirtytwo_grid_unit"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<View
android:id="@+id/tap_stop"
android:layout_width="@dimen/nine_grid_unit"
android:layout_height="@dimen/one_grid_unit"
android:layout_marginTop="@dimen/one_grid_unit"
android:background="@color/grey_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<edeka.digital.app.widget.SegmentedControlView
android:id="@+id/tabSwitchSegmentedControl"
android:layout_width="@dimen/thirtyfive_grid_unit"
android:layout_height="wrap_content"
android:paddingStart="@dimen/three_grid_unit"
android:paddingEnd="@dimen/three_grid_unit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tap_stop"
app:segmentCount="2"
app:segmentTitles="@array/segment_titles_shop_search" />
</androidx.constraintlayout.widget.ConstraintLayout>
...
和代码:
val bottomBehavior = BottomSheetBehavior.from(binding.menuBottomSheet)
bottomBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
val mapView = binding.map
override fun onSlide(bottomSheet: View, slideOffset: Float) {
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
bottomSheetBehaviorObservable.onNext(newState)
when (newState) {
BottomSheetBehavior.STATE_COLLAPSED -> {
mapView.bottom = binding.menuBottomSheet.top
mapView.invalidate()
}
BottomSheetBehavior.STATE_HIDDEN -> {
mapView.bottom = binding.menuBottomSheet.top
mapView.invalidate()
}
else -> { /* void */
}
}
}
})
我希望有某种 resize() 函数,或者如果布局尺寸发生变化,它会自行布局。
我们真正想要的已经在 HERE WeGo 应用程序中实现。如果用户滑动底部工作表,整个 map 会缩放(包括这里的 Logo ):
谁能帮帮我们?
1 中显示的演示可以在这里找到:
最佳答案
我发现实现它的最佳解决方案是添加一个新方法:
private fun updateMapView(bottomSheetTop: Int) {
val mapView = binding.map
val principalY = Math.min(bottomSheetTop / 2.0, mapView.height / 2.0)
mapView.camera.principalPoint = Point2D(mapView.width / 2.0, principalY)
val logoMargin = Math.max(0, mapView.bottom - bottomSheetTop)
mapView.setWatermarkPosition(WatermarkPlacement.BOTTOM_CENTER, logoMargin.toLong())
}
并在 onSlide
和 onStateChanged
中调用它,如下所示:
updateMapView(bottomSheet.top)
请注意,您需要在底部中心位置放置 HERE Logo ,否则无法使用可调整的边距。
我也尝试调整 map View 的大小,但结果并不令人满意。如果你想尝试一下,这里是代码:
private fun updateMapView(bottomSheetTop: Int) {
val mapView = binding.map
mapView.layoutParams.height = bottomSheetTop
mapView.requestLayout()
}
关于android - 这里 SDK Android 在布局更改时调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65610835/