当方向从纵向更改为横向时,应用程序无响应状态

时间:2012-01-30 05:15:01

标签: android

在我的应用程序中,当configurationportrait更改为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();
}

1 个答案:

答案 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的电话....