How to detect orientation change in layout in Android?

后端 未结 10 2345
挽巷
挽巷 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:56

    Create an instance of OrientationEventListener class and enable it.

    OrientationEventListener mOrientationEventListener = new OrientationEventListener(YourActivity.this) {
        @Override
        public void onOrientationChanged(int orientation) {
            Log.d(TAG,"orientation = " + orientation);
        }
    };
    
    if (mOrientationEventListener.canDetectOrientation())
        mOrientationEventListener.enable();
    

提交回复
热议问题