How to detect orientation change in layout in Android?

后端 未结 10 2278
挽巷
挽巷 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条回答
  •  Happy的楠姐
    2020-11-22 09:56

    for Kotilin implementation in the simplest form - only fires when screen changes from portrait <--> landscape if need device a flip detection (180 degree) you'll need to tab in to gravity sensor values

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
    val rotation = windowManager.defaultDisplay.rotation
    
        when (rotation) {            
            0 -> Log.d(TAG,"at zero degree")
            1 -> Log.d(TAG,"at 270 degree")
            2 -> Log.d(TAG,"at 180 degree")
            3 -> Log.d(TAG,"at 90 degree")
    
    
        }
    }
    

提交回复
热议问题