简体   繁体   English

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

[英]How to change orientation to portrait in Fragment Class - Android

My application is in Landscape mode .. I'm trying to change the Fragment in Portrait view .. 我的应用程序处于风景模式..我正在尝试更改肖像视图中的片段。

Used the below code to rotate from landscape to portrait .. 使用以下代码从横向旋转到纵向..

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

But the Fragment "onCreate" and "onCreateView" called twice .. 但是片段“ onCreate”和“ onCreateView”两次调用..

Can anyone please advice on implement Portrait View in Fragment class ? 有人可以在Fragment类中实现Portrait View的建议吗? Or advice to how to avoid calling the onCreateView twice ? 或关于如何避免两次调用onCreateView的建议?

In the Manifest file your activity should adds, 您的活动应在清单文件中添加,

"android:configChanges="keyboardHidden|orientation|screenSize" “机器人:configChanges =” keyboardHidden |方向|屏幕尺寸”

For example, 例如,

<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>

Specifically you should add "screenSize" in the Android manifest file. 具体来说,您应该在Android清单文件中添加“ screenSize”。 So that your Fragment "onCreate" and "onCreateView" won't be called twice. 这样您的片段“ onCreate”和“ onCreateView”将不会被调用两次。

Override setRetainInstance. 覆盖setRetainInstance。 This should solve your problem 这应该可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM