我正在开发一个动态创建控件的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 :(得分: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");
}
}