1

I have created two XML files for portrait and landscape mode separately and kept them in folders layout and layout-land respectively.

Note: I have given android:launchMode = "singleTask" in manifest file for that activity for some reason.

Issue: In both portrait and landscape mode, it takes xml from layout folder. What is the reason for taking xml only from portrait layout folder? Is it because of the "single task property"?. What am I missing in this?

Thanks in advance.

4

1 回答 1

1

通常,当您更改方向时,会触发配置更改事件,您的活动将被销毁并在新布局中重新创建。重新创建时,将使用相应的纵向或横向布局。

因为您表明您明确处理方向更改,所以活动不会在方向更改时销毁/重新创建 - 因此布局不会更改。要实现您想要的,您需要在应用程序启动时存储原始方向,然后在代码中处理方向更改,如下所示:

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;
    }
}

根据您的逻辑,您可能希望从现有视图中获取值并在重新设置布局后重新设置它们。

于 2013-04-30T09:01:15.487 回答