仅在横向模式下运行应用程序?

问题描述:

我想仅在设备上以横向模式运行Hello World示例应用程序。仅在横向模式下运行应用程序?

如果设备更改为人像,我想再举一个烤面包机。例如“请更改为横向模式以运行您的应用程序”。

我该怎么做?

您可以通过编程去两个如下:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

或也清单文件,你可以给这里

<activity android:name=".YourActivity" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"/> 
+1

大ans ....... @ android杀手: – pratik

这在您的清单文件添加到您的活动代码:

android:screenOrientation="landscape" 

这将解决您的方向为横向模式,你需要不显示任何祝酒用户。

在你的manifest资源配置文件(XML)

<activity android:name=".ActivityName" android:screenOrientation="landscape"> 
    </activity> 

指这也Is that possible to check was onCreate called because of orientation change?

可以使用configChanges -attribute你想赶上为屏幕方向变化的活动。

之后,活动中的onConfigurationChanged()-method将在方向更改时调用。

要检查哪个方位当前显示的,请在此较早的帖子:Check orientation on Android phone

之后,你可以炫耀你的消息或做任何你喜欢(当然你也可以将它设置为只在山水秀) 。

你可以尝试这样的事情

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 

Log.v("log_tag", "display width is "+ width); 
Log.v("log_tag", "display height is "+ height); 

if(width<height){ 
    Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG).show(); 
} else { 
    Toast.makeText(getApplicationContext(),"Device is in landscape mode",Toast.LENGTH_LONG).show(); 
}