Android OnConfiguration更改后布局丢失()

Android OnConfiguration更改后布局丢失(),android,Android,我已经找了好一阵子了,但还是不走运。将代码更改为现在编辑的版本。 已编辑 在应用程序上,在onCreate中从以下内容开始: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game_activity); . . . . . 该应用程序仍然可以完美运行。 在onC

我已经找了好一阵子了,但还是不走运。将代码更改为现在编辑的版本。 已编辑 在应用程序上,在onCreate中从以下内容开始:

    @Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_activity);
    . . . . . 
该应用程序仍然可以完美运行。 在onConfigurationChange()中,我删除了setContentView(),因此开关实际上什么都不做:

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
switch(newConfig.orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
    break;
case Configuration.ORIENTATION_LANDSCAPE:
        break;
}
}   //  onConfigurationChanged()  
现在布郎屏幕不见了,这是一种解脱。 但是 当tha app在P模式下启动且配置更改为L模式时,布局与P模式下相同,反之亦然

我突然有了预感! 活动只启动一次!这正是我想要的,但也意味着那里的代码只执行一次。我得先检查一下

编辑

好的,检查了所有需要的东西。但问题依然存在

在纵向模式下启动应用程序时,一切正常。但是,当我切换到横向模式时,配置更改被取消,只有纵向布局仍在使用,反之亦然

对于xml中的不同方向,我确实有不同的布局。 我也有

 android:configChanges="orientation|screenSize" >
在舱单上

我别无选择,任何帮助都将不胜感激

再次编辑

很难证明,但我成功了。 这是一个XML模式,用于纵向模式,一个用于横向模式:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/portGame480x800"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="layout_game_activity"
tools:context=".MeMoInfoActivity" >
.....
</LinearLayout



<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/landGame480x800"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
android:tag="layout-land_game_activity"
tools:context=".MeMoInfoActivity" >
....
</LinearLayout>
这是logcat的输出:

09-30 17:10:32.376: I/MeMoGame(2509): ***************************
09-30 17:10:32.376: I/MeMoGame(2509): MeMoGameActivity onConfigurationChanged
09-30 17:10:32.376: I/MeMoGame(2509): ***************************

09-30 17:10:32.534: I/MeMoGame(2509): LANDSCAPE InfoActivity dispWidthPX: 800


09-30 17:11:02.234: I/MeMoGame(2509): ***************************
09-30 17:11:02.234: I/MeMoGame(2509): MeMoGameActivity onConfigurationChanged
09-30 17:11:02.234: I/MeMoGame(2509): ***************************

09-30 17:11:02.384: I/MeMoGame(2509): Portrait portTag: layout_game_activity

09-30 17:11:02.384: I/MeMoGame(2509): PORTRAIT InfoActivity dispWidthPX: 480

09-30 17:11:02.896: D/dalvikvm(2509): GC_CONCURRENT freed 102K, 3% free 6320K/6471K, paused 19ms+7ms, total 320ms
应用程序是在纵向模式下启动的。很明显,它从未执行过横向布局


有人能帮忙吗?

你应该让Android framework根据当前配置决定使用什么布局。因此,创建两个文件夹,
layout land
layout port
,并放置布局(使用相同的文件名很重要)。另外,删除代码中的开关。

是否有令人信服的理由不让Android使用不同的资源文件夹(例如布局地和布局端口文件夹)处理此问题?@Tanis;我也试过了,它不起作用。如果“它不起作用”意味着你得到相同的白色屏幕,那么我倾向于责怪你的布局本身。非常确定你需要从
configChanges
属性中删除
orientation
,让Android为protrait/landscape使用不同的布局。也试过了,运气不好。我在这个网站上发现了这个想法。这让我充满了希望,但是…你在你的提问中提到了干杯。。。你能不能把你的完整代码或者至少我们现在拥有的更多一点发布出来?谢谢
09-30 17:10:32.376: I/MeMoGame(2509): ***************************
09-30 17:10:32.376: I/MeMoGame(2509): MeMoGameActivity onConfigurationChanged
09-30 17:10:32.376: I/MeMoGame(2509): ***************************

09-30 17:10:32.534: I/MeMoGame(2509): LANDSCAPE InfoActivity dispWidthPX: 800


09-30 17:11:02.234: I/MeMoGame(2509): ***************************
09-30 17:11:02.234: I/MeMoGame(2509): MeMoGameActivity onConfigurationChanged
09-30 17:11:02.234: I/MeMoGame(2509): ***************************

09-30 17:11:02.384: I/MeMoGame(2509): Portrait portTag: layout_game_activity

09-30 17:11:02.384: I/MeMoGame(2509): PORTRAIT InfoActivity dispWidthPX: 480

09-30 17:11:02.896: D/dalvikvm(2509): GC_CONCURRENT freed 102K, 3% free 6320K/6471K, paused 19ms+7ms, total 320ms