在Android中显示配置方向名称

时间:2014-03-18 07:00:38

标签: android

我想在屏幕上显示方向名称。我使用以下代码:

Configuration config = mcontex.getResources().getConfiguration();
Log.d("Configuration","Orientation" + config.orientation);

输出:1

但我想显示Orientation_Potrait或Orientation_Landscape。

2 个答案:

答案 0 :(得分:2)

这应该有效:

final int orientation = mContext.getResources().getConfiguration().orientation;
if (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT == orientation) {
    // do something
} else if (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE == orientation) {
    // do something else
}

请注意,SCREEN_ORIENTATION_PORTRAITSCREEN_ORIENTATION_LANDSCAPE不是唯一可行的选项:

android: screenOrientation = ["unspecified" | "behind" |
    "landscape" | "portrait" |
    "reverseLandscape" | "reversePortrait" |
    "sensorLandscape" | "sensorPortrait" |
    "userLandscape" | "userPortrait" |
    "sensor" | "fullSensor" | "nosensor" |
    "user" | "fullUser" | "locked"]

常量及其整数值可在ActivityInfo类中找到。

答案 1 :(得分:1)

int orient = getResources().getConfiguration().orientation;
switch(orient)
        {

        case  Configuration.ORIENTATION_LANDSCAPE:

            Log.d("Configuration" ,"ORIENTATION_LANDSCAPE");
        break;
        case Configuration.ORIENTATION_PORTRAIT:
            Log.d("Configuration" ,"ORIENTATION_PORTRAIT");
            break;

            default:
            Log.d("Configuration", "default val");
            break;
        }

尝试以上代码......