android screen orientation handling for spinner

折月煮酒 提交于 2021-01-27 06:07:50

问题


I have a activity in which there is a spinner. since for portrait and landscape mode i have different layout so I am changing layout in onConfigurationChanged method

@Override
    public void onConfigurationChanged(Configuration conf) {
        super.onConfigurationChanged(conf);
        setContentView(R.layout.layout);
        initUI();
    } 

but the problem is when I change orientation , my spinner is recreated so if spinner is open in portrait mode it get close in landscape mode.My requirement is : if it is open in any mode , it should be open after orientation change.can you please let me know how to handle this situation.


回答1:


try spinner's performClick() method




回答2:


To stop re-creation of your Spinner you can add this in your Manifest file

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

But by adding this your Layout will not be changed automatically when you rotate your device, so you have to manage that manually like this,

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }

For more information you can check my answer here.




回答3:


Once the orientation changes, destroy method get called and your activity re-create once again. To avoid destroy method get called you need to add below codes in manifest file. But in this case only one layout can be used, if you want to repostion your contents you need to do it dynamically then.

android:configChanges="orientation|keyboardHidden"


来源:https://stackoverflow.com/questions/8830238/android-screen-orientation-handling-for-spinner

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