How to set different orientation of fragment in ac

2019-09-15 07:44发布

问题:

In my MainActivity has fragmentA, fragmentB, fragmentC.

I want set fragmentA and fragmentB orientation to portrait but fragmentC to landscape.

If the Manifest is set to portrait like this:

<activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" />

all fragment orientations are portrait.

How would I set the different orieantation of each fragment?

回答1:

Try this in OnCreateView(View view) of particular fragment,

//ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

  getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

//ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

  getActivity()setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


回答2:

Please add this line in oncreate () in fragment A and B

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

and add this line in oncreate() in fragment C

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

its working for me and I hope it will help you!