layout-land xml files does not work with onConfigurationChanged call back

一个人想着一个人 提交于 2019-12-20 01:52:05

问题


I have different layouts for portrait and landscape mode and I also need to override the onConfigurationChanged() callback. But problem is when I change the phone orientation to landscape my landscape layout does not work.

Can anybody tell me is this onConfigurationChanged call back problem or something else causing that?

Any help will be appreciative.


回答1:


i also need to override the onConfigurationChanged() callback

Why?

but problem is when i change the phone orientation to landscape my landscape layout does not work.

I am going to guess that "does not work" means that the landscape layout does not take effect. This is expected behavior given what you have done.

To resolve this problem, ideally you delete android:configChanges="keyboardHidden|orientation". Putting in that attribute should be done as a last resort, and typically for activities that do not have separate portrait versus landscape layout files.




回答2:


I am sure that it will definitively help you...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
    Toast.makeText(this, "Helloo", Toast.LENGTH_SHORT).show();
}
enter code here
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);

    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        setContentView(R.layout.main_land);
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        setContentView(R.layout.main);
        break;
    }
}

@Override
public Object onRetainNonConfigurationInstance() {
    // TODO Auto-generated method stub
    return super.onRetainNonConfigurationInstance();
}

}

and add this line in your manifest file.. android:configChanges="keyboardHidden|orientation"



来源:https://stackoverflow.com/questions/5336614/layout-land-xml-files-does-not-work-with-onconfigurationchanged-call-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!