【问题标题】:portrait and landscape orientations and set android:configChanges="orientation" in fragment纵向和横向方向并在片段中设置 android:configChanges="orientation"
【发布时间】:2014-06-20 11:49:24
【问题描述】:

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

有什么办法可以实现这个..

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 是的,但我需要在其中重新创建视图并处理它的事件。我有一个 web 服务 onCreateView()。不想打电话,只是有使用布局文件的方向功能
  • 当您将 configChanges 添加到 manifest 时,onConfigurationChanged() 方法会在方向更改但未重新创建活动时调用。您需要自己更改任何布局。
  • 已编辑答案,包括如何设置新布局。
  • 好的。正如你所说,我需要自己添加布局,我需要初始化对象和 Widget Click 并列出来自 Web 服务的数据。这是在方向更改时一次又一次地调用 Web 服务的好方法吗?
  • 如果你把 android:configChanges 添加到你的 manifest,它不会一次又一次地调用 web 服务。
【解决方案2】:

如您所知,如果您设置 android:configChanges="orientation" 然后在方向更改活动时,片段将不会重新启动。

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

因此,您想要实现的并不是理想的方法。

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

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 2019-02-08
    • 2013-06-12
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多