我希望我的Android应用程序仅在肖像模式下运行?
-
03-10-2019 - |
题
我希望我的Android应用程序仅在肖像模式下运行?我怎样才能做到这一点?
解决方案
在清单中,为您的所有活动设置此:
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
让我解释:
- 和
android:configChanges="orientation"
您告诉Android您将对方向的变化负责。 android:screenOrientation="portrait"
您设置默认方向模式。
其他提示
在Android清单文件中,为您的 <activity>
那 android:screenOrientation="portrait"
有两种方法,
- 添加
android:screenOrientation="portrait"
对于清单文件中的每个活动 - 添加
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
在每个Java文件中。
我知道的老帖子。为了使您的应用程序始终处于肖像模式下功能在设备上组织。
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
;-)
我用
android:screenOrientation="nosensor"
如果您不想支持向上的肖像模式,这将很有帮助。
不隶属于 StackOverflow