简体   繁体   English

纵向和横向方向,并在片段中设置android:configChanges =“ orientation”

[英]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.

相关问题 Android - 纵向和横向? - Android - portrait and landscape orientations? 如何在 Android 中只允许一个特定片段同时具有纵向和横向方向? - How do I allow only one specific fragment to have both portrait and landscape orientations in Android? 在最初将屏幕方向更改为 LANDSCAPE 时,触发传感器方向然后加载到 PORTRAIT - On changing screen orientation to LANDSCAPE initially, triggers sensor orientations then loads in PORTRAIT 在纵向方向机器人的风景布局 - Landscape layout in portrait orientation android android:configChanges =“screenSize”参数在横向模式下采用纵向高度 - android:configChanges=“screenSize” parameter is taking portrait height in landscape mode 如何使用片段管理横向和纵向的方向 - How to manage orientation in landscape and portrait using fragment 如何设置几个片段是横向模式和休息应该是Android中的肖像? - How to set few fragment be landscape mode and rest should be portrait in android? 有没有办法禁用纵向倒置方向,但启用常规纵向和横向方向? - Is there a way to disable portrait-upside down orientation but enable regular portrait and landscape orientations? 将人像旋转到风景时,我要关闭力量,我尝试了android:configChanges =“ keyboard | keyboardHidden”并以人像模式工作吗? - Getting Force Close when rotating portrait to landscape and I tried android:configChanges=“keyboard|keyboardHidden” and working in portrait mode? 如何将MediaRecorder方向设置为横向或纵向 - How to set MediaRecorder orientation to landscape or portrait
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM