我希望我的Android应用程序只能在纵向模式下运行?我怎样才能做到这一点?
在清单中,为您的所有活动设置此项:
<activity android:name=".YourActivity" android:configChanges="orientation" android:screenOrientation="portrait"/>
让我解释:
使用 android:configChanges="orientation" ,您告诉Android您将负责方向的更改 .
android:configChanges="orientation"
android:screenOrientation="portrait" 您设置了默认方向模式 .
android:screenOrientation="portrait"
在Android清单文件中,将 <activity> 的属性设为 android:screenOrientation="portrait"
<activity>
有两种方式,
为清单文件中的每个活动添加 android:screenOrientation="portrait"
在每个java文件中添加 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); .
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
老帖子我知道 . 为了在纵向模式下运行您的应用程序,即使可能或交换方向等(例如在平板电脑上)我设计了这个功能,用于将设备设置在正确的方向,而无需知道如何纵向和横向功能在设备上组织 .
private void initActivityScreenOrientPortrait() { // Avoid screen rotations (use the manifests android:screenOrientation setting) // Set this to nosensor or potrait // Set window fullscreen this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); DisplayMetrics metrics = new DisplayMetrics(); this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); // Test if it is VISUAL in portrait mode by simply checking it's size boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); if( !bIsVisualPortrait ) { // Swap the orientation to match the VISUAL portrait mode if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ) { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); } } else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); } }
奇迹般有效!
注意:根据您的活动更改 this.activity 或将其添加到主活动并删除 this.activity ;-)
this.activity
我用
android:screenOrientation="nosensor"
如果您不想支持侧面向下纵向模式,这将很有帮助 .
6 回答
在清单中,为您的所有活动设置此项:
让我解释:
使用
android:configChanges="orientation"
,您告诉Android您将负责方向的更改 .android:screenOrientation="portrait"
您设置了默认方向模式 .在Android清单文件中,将
<activity>
的属性设为android:screenOrientation="portrait"
有两种方式,
为清单文件中的每个活动添加
android:screenOrientation="portrait"
在每个java文件中添加
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
.老帖子我知道 . 为了在纵向模式下运行您的应用程序,即使可能或交换方向等(例如在平板电脑上)我设计了这个功能,用于将设备设置在正确的方向,而无需知道如何纵向和横向功能在设备上组织 .
奇迹般有效!
注意:根据您的活动更改
this.activity
或将其添加到主活动并删除this.activity
;-)我用
如果您不想支持侧面向下纵向模式,这将很有帮助 .
在清单中,为您的所有活动设置此项: