转动模拟器后布局发生变化,但不是设备

Layout changes after turning the emulator, but not the device

当方向为纵向时,我需要在 RecyclerView 中显示 2 列,当方向为 land 时,我需要显示 4 列。它在模拟器上工作正常。

RecyclerView 为纵向。

开启模拟器:

如您所见,有刷新按钮。单击它后,我们会在屏幕上看到 4 列:

但是当我 运行 我的手机上的项目 phone 时,没有刷新按钮,我在陆地方向看到 2 列。

这是我的部分代码:

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
        previewImages.setLayoutManager(new GridLayoutManager(this, 4));
    }
    else {
        previewImages.setLayoutManager(new GridLayoutManager(this, 2));
    }
}

我在清单中添加了这一行:

android:configChanges="orientation"

那么,问题是什么,我该如何解决?

你能不能像这样更改 onConfigurationChanged 方法中的代码块。也许可以提供帮助。

new GridLayoutManager(context, 1, GridLayoutManager.HORIZONTAL, false)