我有一个Activity和几个片段(它们被称为fragmentA和fragmentB)。现在fragmentA需要有纵向和横向布局。fragmentB必须保持肖像。Activity在onCreate方法中显示fragmentA。我尝试了这个链接:https://stackoverflow.com/a/53864286/15351040在fragmentA中添加了以下代码。
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onDestroyView() {
super.onDestroyView()
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
}
并在fragmentB中添加了以下代码。
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onDestroyView() {
super.onDestroyView()
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
}
但是当我打开fragmentB时,fragmentA再次打开,而不是fragmentB。当activity的requestOrientation被改变时,可能会调用它的onCreate。当fragmentA是肖像时,它工作得很好,但不是风景。
我现在该怎么办?
2条答案
按热度按时间s6fujrry1#
vom3gejh2#
为AndroidManifest.xml中的Activity添加带有方向值的“修改”标签。举例来说: