我一直在尋找一段時間,但仍然沒有運氣。將代碼更改爲現在編輯的版本。 EDITED 應用中的onCreate開始與此:onConfigurationChanged()後失去佈局
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
. . . . .
的應用程序仍然工作完美無瑕。 在onConfigurationChange()我刪除了的setContentView(),所以開關actualy做什麼都沒有:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
switch(newConfig.orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
break;
case Configuration.ORIENTATION_LANDSCAPE:
break;
}
} // onConfigurationChanged()
現在布蘭克屏幕都走了,這是一個緩解。
但是; 當應用程序以P模式啓動並且配置更改爲L模式時,佈局保持與P模式相同,反之亦然。
我突然有一種預感! 活動只開始一次!這正是我想要的,但它也意味着那裏的代碼只執行一次。我必須先檢查一下。
編輯
OK,檢查了所有的必要。但問題依然存在。
以縱向模式啓動應用程序時,每件事情都很好。但是,當我切換到橫向模式時,配置更改將被刪除,只有縱向佈局仍在使用,反之亦然。
我對xml中的不同方向有不同的佈局。 我也有
android:configChanges="orientation|screenSize" >
在清單中。
我沒有選擇,任何幫助將不勝感激。
再次編輯
這是很難證明,但我succeded。 這是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用不同的資源文件夾處理這個問題(例如layout-land和layout-port文件夾)? –
@Tanis;我也嘗試過,它不會工作。 – PageMaker
如果「它不起作用」意味着你獲得了相同的白色屏幕,那麼我傾向於責怪你自己的佈局。 –