Android方向改变
我正在做一个项目。在这里,我使用了屏幕方向、横向和纵向。我还使用了Android方向改变,android,Android,我正在做一个项目。在这里,我使用了屏幕方向、横向和纵向。我还使用了android:configChanges=“keyboardHidden | orientation | screenSize”,这样在切换方向时活动不会刷新 现在,由于我使用configChanges=“orientation”,应用程序无法从布局端口->布局和xml布局切换 我可以从横向->纵向或纵向->横向切换,但它从两个方向显示布局端口xml,而不是从布局端口->布局地或 布局用地->布局端口。将此添加到您的活动中 in
android:configChanges=“keyboardHidden | orientation | screenSize”
,这样在切换方向时活动不会刷新
现在,由于我使用configChanges=“orientation”,应用程序无法从布局端口->布局和xml布局切换
我可以从横向->纵向或纵向->横向切换,但它从两个方向显示布局端口xml,而不是从布局端口->布局地或布局用地->布局端口。将此添加到您的活动中
inside of oncreate()
if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
setContentView(R.layout.land);
else if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT)
setContentView(R.layout.port);
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.land);
} else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.port);
}
}
在AndroidManifest文件中,仅添加android:configChanges=“orientation”并删除要处理此方向的活动的其他两个 更改android:configChanges=“keyboardHidden |方向|屏幕大小” 到android:configChanges=“orientation” 在您的活动中,覆盖
onConfigurationChanged
methode
就像Manivannan在他的回答中描述的那样
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//set your landscape layout
} else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
//the same for portrait layout
}
}
就是这样。我正在使用layout和layout land,但它在land和port view中显示布局xml,而不是切换到layout-land。您不能使用android:configChanges=“keyboardHidden | orientation | screenSize”来处理方向更改。。查看SaveInstanceState和OnRestoreInstanceState让我们澄清一下
android:configChanges=“orientation”
是一种黑客行为。它只能在非常特殊的情况下使用。如果你搜索它为什么不好(非常糟糕),会有很多讨论。简言之,您可以掩盖稍后将在其他场景中出现的bug。正确的答案是学习活动生命周期,并据此编写代码。这种黑客行为是懒惰的人掩盖代码中的问题的一种方式,这些问题仍在等待被发现。谢谢…我跟随你的答案…如果我只使用android:configChanges=“orientation”在方向更改时重新加载活动..我不想在方向更改时重新加载活动,而是将屏幕大小添加到android:ConfigChanges在API级别13或更高版本中,当方向更改时屏幕大小会更改,因此这仍然会导致在方向更改时销毁并启动活动