我希望我的游戏能够固定在Portrait上我该怎么做?

时间:2015-08-14 20:42:54

标签: java android

所以我想看看我的项目在风景中的样子,并且肯定我不喜欢外观。我想知道如何让我的项目留在肖像上?我将不胜感激。

android.app.FragmentManager myFragment = getFragmentManager();
        FragmentTransaction fragmentTransaction = myFragment.beginTransaction();

        Configuration configInfo = getResources().getConfiguration();
        if(configInfo.orientation ==Configuration.ORIENTATION_LANDSCAPE){
            FragmentLandscape lScape = new FragmentLandscape();
            fragmentTransaction.replace(android.R.id.content,lScape);
        }else{

            FragmentPortrait lportrait = new FragmentPortrait();
            fragmentTransaction.replace(android.R.id.content,lportrait);
        }
        fragmentTransaction.commit();

1 个答案:

答案 0 :(得分:2)

有多种强制纵向模式的方法,但我首选的方法是对AndroidManifest.xml中的每个活动使用以下代码

android:screenOrientation="portrait"

稍后,如果您决定为平板电脑创建备用横向布局,则可以将AndroidManifest.xml中的相同行更改为:

android:screenOrientation="nosensor"

“nosensor”设置告诉您的应用使用设备的自然方向,并且如果用户旋转设备则不会更改。因此平板电脑将始终为您的应用程序使用横向方向,而手机将始终使用纵向方向。