How to detect orientation change in layout in Android?

后端 未结 10 2275
挽巷
挽巷 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 09:57

    For loading the layout in layout-land folder means you have two separate layouts then you have to make setContentView in onConfigurationChanged method.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.yourxmlinlayout-land);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            setContentView(R.layout.yourxmlinlayoutfolder);
        }
    }
    

    If you have only one layout then no necessary to make setContentView in This method. simply

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
    

提交回复
热议问题