2012-06-01 99 views
0

我正在開發一個動態創建控件的Android應用程序。我做了這種類型的編碼。Android:幫助處理屏幕方向

TextView lblTitle = new TextView(myContext); 
relLayoutHeader.addView(lblTitle); 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     lblTitle.settext("LandScape"); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     lblTitle.settext("Portrait"); 
} 

清單文件:

android:configChanges="orientation|keyboardHidden" 

當我從縱向更改方向爲橫向它的作品好。但從風景到肖像應用程序崩潰了。部隊關閉。

任何建議我的代碼?????

+0

如果你說它崩潰了,請添加你的logcat ..只要你的應用程序崩潰,你肯定會得到一個崩潰日誌。 – Ghost

回答

0

您需要重新初始化您在onConfigurationChanged處查看。

// used in onCreate() and onConfigurationChanged() to set up the UI elements 
public void InitializeUI() { 
    // get views from ID's 
    relLayoutHeader = _______Initialise_here; 
    TextView lblTitle = new TextView(myContext); 
    relLayoutHeader.addView(lblTitle); 
    // etc... hook up click listeners, whatever you need from the Views 
} 

// Called when the activity is first created. 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    InitializeUI(); 
} 

// this is called when the screen rotates. 
// (onCreate is no longer called when screen rotates due to manifest, see: 
// android:configChanges) 
@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    setContentView(R.layout.main); 

    InitializeUI(); 
    //And then do your stuff 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      lblTitle.settext("LandScape"); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     lblTitle.settext("Portrait"); 
    } 
}