如何在Fragment类中将方向更改为纵向-Android

如何在Fragment类中将方向更改为纵向-Android,android,android-activity,android-fragments,Android,Android Activity,Android Fragments,我的应用程序处于横向模式。。我正在尝试更改纵向视图中的片段 使用以下代码从横向旋转到纵向 getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 但是片段“onCreate”和“onCreateView”调用了两次 任何人都可以在Fragment类中实现纵向视图吗?或者关于如何避免调用onCreateView两次的建议?覆盖setRetainInstance。这将解决您的问题在活动应添加的

我的应用程序处于横向模式。。我正在尝试更改纵向视图中的片段

使用以下代码从横向旋转到纵向

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
但是片段“onCreate”和“onCreateView”调用了两次


任何人都可以在Fragment类中实现纵向视图吗?或者关于如何避免调用onCreateView两次的建议?

覆盖setRetainInstance。这将解决您的问题

在活动应添加的清单文件中

“android:configChanges=“keyboardHidden |方向|屏幕大小”

比如说,

<activity
    android:name=".MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:label="@string/app_name"
    android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>


具体来说,您应该在Android清单文件中添加“screenSize”。这样您的片段“onCreate”和“onCreateView”就不会被调用两次。

您将上述代码放在哪里了?请查看setRatainInstance(布尔值)method请向我们展示更多的代码,特别是您的片段或类。@Eenvincible在onCreateView中我放置了代码片段。@janpan这是我的片段:您能粘贴剪下的代码以进行检查吗?因为我已在onCreate方法中设置了RetainInstance(true)。。