我想在基于按钮导航的布局中使用全屏片段。问题在于内容没有填充隐藏的导航空间。我的目标是使它填充整个屏幕,而无需导航或状态栏。
活动xml:
<com.flipboard.bottomsheet.BottomSheetLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomsheet"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>
片段代码:(来自Android Studio的全屏片段模板)
class FullscreenFragment : Fragment() {
private val hideHandler = Handler()
@Suppress("InlinedApi")
private val hidePart2Runnable = Runnable {
// Delayed removal of status and navigation bar
// Note that some of these constants are new as of API 16 (Jelly Bean)
// and API 19 (KitKat). It is safe to use them, as they are inlined
// at compile-time and do nothing on earlier devices.
val flags =
View.SYSTEM_UI_FLAG_LOW_PROFILE or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
activity?.window?.decorView?.systemUiVisibility = flags
(activity as? AppCompatActivity)?.supportActionBar?.hide()
}
private val showPart2Runnable = Runnable {
// Delayed display of UI elements
fullscreenContentControls?.visibility = View.VISIBLE
}
private var visible: Boolean = false
private val hideRunnable = Runnable { hide() }
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
private val delayHideTouchListener = View.OnTouchListener { _, _ ->
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS)
}
false
}
private var dummyButton: Button? = null
private var fullscreenContent: View? = null
private var fullscreenContentControls: View? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_fullscreen, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
visible = true
dummyButton = view.findViewById(R.id.dummy_button)
fullscreenContent = view.findViewById(R.id.fullscreen_content)
fullscreenContentControls = view.findViewById(R.id.fullscreen_content_controls)
// Set up the user interaction to manually show or hide the system UI.
fullscreenContent?.setOnClickListener { toggle() }
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
dummyButton?.setOnTouchListener(delayHideTouchListener)
}
override fun onResume() {
super.onResume()
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100)
}
override fun onPause() {
super.onPause()
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
// Clear the systemUiVisibility flag
activity?.window?.decorView?.systemUiVisibility = 0
show()
}
override fun onDestroy() {
super.onDestroy()
dummyButton = null
fullscreenContent = null
fullscreenContentControls = null
}
private fun toggle() {
if (visible) {
hide()
} else {
show()
}
}
private fun hide() {
// Hide UI first
fullscreenContentControls?.visibility = View.GONE
visible = false
// Schedule a runnable to remove the status and navigation bar after a delay
hideHandler.removeCallbacks(showPart2Runnable)
hideHandler.postDelayed(hidePart2Runnable, UI_ANIMATION_DELAY.toLong())
}
@Suppress("InlinedApi")
private fun show() {
// Show the system bar
fullscreenContent?.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
visible = true
// Schedule a runnable to display UI elements after a delay
hideHandler.removeCallbacks(hidePart2Runnable)
hideHandler.postDelayed(showPart2Runnable, UI_ANIMATION_DELAY.toLong())
(activity as? AppCompatActivity)?.supportActionBar?.show()
}
/**
* Schedules a call to hide() in [delayMillis], canceling any
* previously scheduled calls.
*/
private fun delayedHide(delayMillis: Int) {
hideHandler.removeCallbacks(hideRunnable)
hideHandler.postDelayed(hideRunnable, delayMillis.toLong())
}
companion object {
/**
* Whether or not the system UI should be auto-hidden after
* [AUTO_HIDE_DELAY_MILLIS] milliseconds.
*/
private const val AUTO_HIDE = true
/**
* If [AUTO_HIDE] is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private const val AUTO_HIDE_DELAY_MILLIS = 3000
/**
* Some older devices needs a small delay between UI widget updates
* and a change of the status and navigation bar.
*/
private const val UI_ANIMATION_DELAY = 300
}
}
答案 0 :(得分:3)
根据这个BottomNavigationView问题帖子:
https://github.com/material-components/material-components-android/issues/499#issuecomment-519618931
bottomNavigationView.setOnApplyWindowInsetsListener(null);
上面的代码示例可能会解决这个问题。
答案 1 :(得分:0)
我使用以下代码解决了这个问题:
@Suppress("InlinedApi")
private val hidePart2Runnable = Runnable {
val flags =
(View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
activity?.window?.decorView?.systemUiVisibility = flags
(activity as? AppCompatActivity)?.apply {
supportActionBar?.hide()
nav_view?.isVisible = false
}
}
并在表演中:
// Show the system bar
player_view?.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// Schedule a runnable to display UI elements after a delay
hideHandler.removeCallbacks(hidePart2Runnable)
(activity as? AppCompatActivity)?.apply {
supportActionBar?.show()
nav_view?.isVisible = true
}
答案 2 :(得分:-2)
您无法隐藏状态栏,并且如果您隐藏底部导航,用户将如何导航到其他片段?
如果您只想隐藏工具栏,请在包含底部导航的活动中使用此样式
<style name="full_screen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimary</item>
</style>