我想在屏幕上显示方向名称。我使用以下代码:
Configuration config = mcontex.getResources().getConfiguration();
Log.d("Configuration","Orientation" + config.orientation);
输出:1
但我想显示Orientation_Potrait或Orientation_Landscape。
答案 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_PORTRAIT
和SCREEN_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;
}
尝试以上代码......