gpt4 book ai didi

android - onConfigurationChanged() 后布局丢失

转载 作者:行者123 更新时间:2023-11-29 17:56:23 28 4
gpt4 key购买 nike

我已经搜索了很长时间,但仍然没有运气。将代码更改为现在编辑的版本。已编辑在 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 应用程序在 P 模式下启动并且配置更改为 L 模式时,布局与 P 模式下保持相同,反之亦然。

我突然有种预感! Activity 只开始一次!这正是我想要的,但这也意味着那里的代码只执行一次。我必须先检查一下。

编辑

好的,检查所有必要的。但问题依然存在。

当以纵向模式启动应用程序时,一切都很好。但是,当我更改为横向模式时,仅进行了配置更改,仍然使用纵向布局,反之亦然。

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

 android:configChanges="orientation|screenSize" >

在 list 中。

我别无选择,我们将不胜感激。

再次编辑

这很难证明,但我成功了。这是在 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>

这是代码:

public void onConfigurationChanged(Configuration newConfig) 
{
super.onConfigurationChanged(newConfig);

Log.i(TAG, "***************************\nMeMoGameActivity onConfigurationChanged\n***************************");

switch(newConfig.orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
LinearLayout portLayout = (LinearLayout) findViewById(R.id.portGame480x800);
if(portLayout != null)
{
String portTag = (String) portLayout.getTag().toString();
Log.i(TAG, "Portrait portTag: "+portTag);
}
break;
case Configuration.ORIENTATION_LANDSCAPE:
Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show();
LinearLayout landLayout = (LinearLayout) findViewById(R.id.landGame480x800);
if(landLayout != null)
{
String landTag = (String) landLayout.getTag().toString();

Log.i(TAG, "LansScape landTag: "+landTag);
}
break;
}
} // onConfigurationChanged()

这是 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 框架根据当前配置决定使用什么布局。所以创建 2 个文件夹,layout-landlayout-port,并放置您的布局(使用相同的文件名很重要)。此外,删除代码中的开关。

关于android - onConfigurationChanged() 后布局丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com