我分别为纵向和横向模式创建了两个XML文件,并将它们分别保存在文件夹布局和布局区域中。
注意:出于某种原因,我在清单文件中为此活动指定了android:launchMode =“ singleTask”。
问题:在纵向和横向模式下,它都从布局文件夹中获取xml。
为什么只从纵向布局文件夹中获取xml是什么原因?是因为“单个任务属性”吗?我在这里想念什么?
提前致谢。
最佳答案
通常,当您更改方向时,会触发配置更改事件,活动将被销毁并在新布局中重新创建。重新创建时,将使用相应的纵向或横向布局。
因为您指示您显式处理了方向更改,所以不会在方向更改时破坏/重新创建活动-因此布局也不会更改。为了实现所需的功能,您需要在应用启动时存储原始方向,然后处理代码中的方向更改,如下所示:
private int currentOrientation;
public void onCreate(Bundle sis) {
...
currentOrientation = getResources().getConfiguration().orientation;
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(currentOrientation != newConfig.orientation) {
//re-set the layout into your activity
setContentView(R.layout.my_layout);
currentOrientation = newConfig.orientation;
}
}
根据您的逻辑,您可能希望从现有视图中获取值,并在重新设置布局后重新设置它们。