当设备方向改变时,xml不会切换

Lab*_*lan 20 configuration android android-layout


我做了两个文件夹,res/layoutres/layout-land

我得到的输出
如果我在portrait模式下启动应用程序,如果应用程序在模式下运行,它将始终使用layout文件夹中的xml portrait.并且layout-land如果我将设备更改为landscape模式,则不会使用xml
如果它以模式启动landscape它只使用xml in layout-land
xml不会在方向改变时切换

我期待的是
它应该layout在纵向模式下使用文件夹中的xml并在横向模式下使用布局中的xml

在我android:configChanges="orientation"为活动添加的清单文件中

<supports-screens 
        android:resizeable="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:anyDensity="true" />
Run Code Online (Sandbox Code Playgroud)

我错过了这里的任何东西吗?我需要做些什么改变?
谢谢

Sid*_*ele 39

清单代码

android:configChanges="orientation|screenSize"
Run Code Online (Sandbox Code Playgroud)

忽略"layout-land"中的XML并使用"layout"文件夹中的XML.如果为横向创建不同的XML,请不要使用该android:configChanges="orientation|screenSize"活动的标记.

  • 您知道我们如何检测在这种情况下是否发生方向变化?如果没有此设置onConfigurationChange永远不会被调用. (3认同)

Bar*_*vdh 23

android:configChanges ="orientation"阻止活动重新启动,因此重新加载xml布局(通常在onCreate中执行此操作).而是调用onConfigurationChanged(newConfig).所以你可以这样做:

@Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.<xml file>);
    }
Run Code Online (Sandbox Code Playgroud)

如果可用的话,这将从layout-land目录重新加载布局.注意:您还需要将操作链接到按钮和类似的东西

  • 如果我有碎片怎么办? (4认同)