我的项目使用导航框架,layout_home.xm l将同时加载UIFragmentCamera.xml和layout_fragment_camera_ui.xml]
我删除了所有操作栏,并使用代码UIHome.kt将窗口设置为全屏,所以我可以像Image 1一样获得全屏。
当我单击按钮btnMenu时将显示弹出菜单,但该窗口还会显示操作栏,您可以看到图像2。
单击btnMenu弹出菜单时如何将窗口设置为全屏?
UIHome.kt
private const val IMMERSIVE_FLAG_TIMEOUT = 500L
const val FLAGS_FULLSCREEN = 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
class UIHome : AppCompatActivity() {
private lateinit var mContext: Context
private lateinit var container: FrameLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_home)
container = findViewById(R.id.fragment_container)
}
override fun onResume() {
super.onResume()
container.postDelayed({
container.systemUiVisibility = FLAGS_FULLSCREEN
}, IMMERSIVE_FLAG_TIMEOUT)
}
}
UIFragmentCamera.kt
class UIFragmentCamera : Fragment() {
private lateinit var container: ConstraintLayout
private lateinit var viewFinder: TextureView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.layout_fragment_camera, container, false)
@SuppressLint("MissingPermission")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
container = view as ConstraintLayout
viewFinder = container.findViewById(R.id.view_finder)
viewFinder.post {
View.inflate(requireContext(), R.layout.layout_fragment_camera_ui, container)
btnMenu.setOnClickListener{
v -> showPopup(v, requireContext())
}
}
}
private fun showPopup(v: View, mContext: Context) {
val popup = PopupMenu(mContext, v)
popup.inflate(R.menu.menu_popup_more)
popup.setOnMenuItemClickListener {
item -> handlePopupMenu(item, mContext)
}
popup.show()
}
private fun handlePopupMenu(item: MenuItem, mContext: Context): Boolean {
when (item.itemId) {
R.id.MenuMoreAbout->{
requireActivity().openActivity<UIAbout>()
}
}
return false
}
}
layout_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:id="@+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</FrameLayout>
UIFragmentCamera.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera_container"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/view_finder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout_fragment_camera_ui.xml
<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:id="@+id/camera_ui_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cutout_safe_area"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/btnMenu"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="@dimen/margin_xsmall"
android:layout_marginTop="@dimen/margin_small"
android:layout_width="@dimen/round_button_small"
android:layout_height="@dimen/round_button_small"
android:scaleType="fitCenter"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_back" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
图像1
图像2
您应该在AndroidManifest中设置主题,而不是在活动onResume()生命周期方法中设置主题
<activity
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
....
>