Check orientation on Android phone

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

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

23条回答
  •  盖世英雄少女心
    2020-11-22 07:05

    The current configuration, as used to determine which resources to retrieve, is available from the Resources' Configuration object:

    getResources().getConfiguration().orientation;
    

    You can check for orientation by looking at its value:

    int orientation = getResources().getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // In landscape
    } else {
        // In portrait
    }
    

    More information can be found in the Android Developer.

提交回复
热议问题