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

时间:2014-06-20 11:49:25

标签: android android-layout android-fragments fragment orientation

我有特定的布局&纵向和横向方向的layout-land文件,并将android:configChanges="orientation"设置为清单文件中的活动,以阻止oncreate()在片段中执行。 Android会自动销毁并重新创建方向更改的活动,因此每次调用onCreate()都会获得正确的布局。 我有Main ActivityFragment,其中包含4个Tab,其中包含片段作为方向更改onCreate()来电并重新创建它。但我不想接到onCreate()的电话,但需要使用布局&布局 - 土地文件。

有没有办法实现这个目标..

2 个答案:

答案 0 :(得分:0)

使用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 );
    }
}

答案 1 :(得分:0)

如您所知,如果您设置android:configChanges =“orientation”然后在方向上更改活动,反过来不会重新启动片段。

因此,如果您处于纵向模式并更改为横向模式,它将保留您的纵向布局本身。所以你的横向模式布局(来自layout-land)将不会被使用。从横向模式到纵向模式也是如此。

所以,你想要实现的并不是理想的方法。

由于您希望更改布局,因此必须保存每个片段的状态(数据), onConfigChanges 将数据填充回来,以便选择适当的布局。