[英]portrait and landscape orientations and set android:configChanges=“orientation” in fragment
I have specific layout & layout-land files for both portrait and landscape orientations and set android:configChanges="orientation"
to the activity in manifest file to stop oncreate()
to execute in fragment. 我对纵向和横向都有特定的布局和布局土地文件,并将
android:configChanges="orientation"
设置为清单文件中的活动,以停止oncreate()
在片段中执行。 Android will automatically destroy and recreate activity on an orientation change, and so each call to onCreate()
will get the right layout. Android将在方向改变时自动销毁并重新创建活动,因此每次调用
onCreate()
都会获得正确的布局。 I have Main ActivityFragment which contains 4 Tab having fragments as orientation changes onCreate()
get call and re-create it.But I dont want to get call to onCreate()
but need the layout orientation change using layout & layout-land files. 我有Main ActivityFragment,其中包含4个选项卡,其中的片段随着方向的变化而被
onCreate()
调用并重新创建它。但是我不想被调用到onCreate()
但是需要使用layout&layout-land文件更改布局方向。
Is any way to achieve this.. 有什么办法可以做到这一点。
The following method is called when you override using configChanges. 使用configChanges覆盖时,将调用以下方法。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Check the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_layout);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.activity_layout );
}
}
As you know, if you set android:configChanges="orientation" Then on orientation change the activity and in turn fragments are not going to be restarted. 如您所知,如果您设置android:configChanges =“ orientation”,则在方向更改活动后,片段将不会重新启动。
So, if you are in portrait mode and change to landscape mode, it will retain your portrait layout itself. 因此,如果您处于纵向模式并更改为横向模式,它将保留纵向布局本身。 so your landscape mode layout(from layout-land) will not be used.
因此不会使用您的风景模式布局(来自layout-land)。 And similarly in case from landscape mode to portrait mode.
同样,从风景模式到肖像模式也是如此。
So, What you are trying to achieve is not the ideal approach. 因此,您要实现的目标不是理想的方法。
Since, you want the layout to change, you must save the states(data) of each fragment and onConfigChanges populate the data back so that appropriate layout is selected. 由于要更改布局,因此必须保存每个片段的状态(数据),然后onConfigChanges将数据填充回去,以便选择适当的布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.