How to detect orientation change in layout in Android?

后端 未结 10 2334
挽巷
挽巷 2020-11-22 09:36

I just implemented a orientation change feature - e.g. when the layout changes from portrait to landscape (or vice versa). How can I detect when the orientation change event

10条回答
  •  感动是毒
    2020-11-22 10:06

    I just want to propose another alternative that will concern some of you, indeed, as explained above:

    android:configChanges="orientation|keyboardHidden" 
    

    implies that we explicitly declare the layout to be injected.

    In case we want to keep the automatic injection thanks to the layout-land and layout folders. All you have to do is add it to the onCreate:

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getSupportActionBar().hide();
    
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            getSupportActionBar().show();
        }
    

    Here, we display or not the actionbar depending on the orientation of the phone

提交回复
热议问题