Check orientation on Android phone

前端 未结 23 1645
庸人自扰
庸人自扰 2020-11-22 06:26

How can I check if the Android phone is in Landscape or Portrait?

23条回答
  •  太阳男子
    2020-11-22 07:19

    Another way of solving this problem is by not relying on the correct return value from the display but relying on the Android resources resolving.

    Create the file layouts.xml in the folders res/values-land and res/values-port with the following content:

    res/values-land/layouts.xml:

    
    
        true
    
    

    res/values-port/layouts.xml:

    
    
        false
    
    

    In your source code you can now access the current orientation as follows:

    context.getResources().getBoolean(R.bool.is_landscape)
    

提交回复
热议问题