在我的应用程序中,当configuration
从portrait
更改为landscape
进入配置更改的代码片段是:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mWordSelCategory =mWordCategory;
Log.d(TAG,"THE CATEGORY IS :: "+mWordSelCategory);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.word_start);
mImagebackground = (ImageView) findViewById(R.id.mImgWord_ImageBg);
if (mWordSelCategory.equals("Animals")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_animals);
} else if (mWordSelCategory.equals("Fruits")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_fruits);
} else if (mWordSelCategory.equals("Things")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_objects);
} else if (mWordSelCategory.equals("Vegetables")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_vegetables);
}
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.word_start_horizt);
mImagebackground = (ImageView) findViewById(R.id.mImgWord_ImageBg);
if (mWordSelCategory.equals("Animals")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_hori_animals);
} else if (mWordSelCategory.equals("Fruits")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_horiz_fruits);
} else if (mWordSelCategory.equals("Things")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_hori_objects);
} else if (mWordSelCategory.equals("Vegetables")) {
mImagebackground
.setBackgroundResource(R.drawable.word_start_hori_vegetable);
}
}
getReconfigData();
}
答案 0 :(得分:1)
哇,你那里有很多代码重复。
请参阅http://developer.android.com/guide/topics/resources/providing-resources.html并特别查看提供替代资源部分 - 您可以让布局系统使用不同的布局,具体取决于您的设备是自动处于横向还是纵向模式。 / p>
E.g。
res/layout/word_start.xml
res/layout-land/word_start.xml
然后,您只需拨打setContentView
和所有findViewById
的电话....