Setting the orientation for only 1 fragment in my activity while the rest is in portrait

后端 未结 8 1091
难免孤独
难免孤独 2020-12-05 01:52

My app needs to be in portrait mode so I set it in the manifest by:

android:screenOrientation=\"portrait\"

But I just recently added anothe

8条回答
  •  不思量自难忘°
    2020-12-05 02:19

    This is old question but answering this just incase anyone wanted the solution as the OP needed. The simple way to achieve this is as follows.

    public class LandscapeFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
           getActivity().setRequestedOrientation(
                  ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    
       @Override
       public View onDestroyView() {
           super.onDestroyView();
    
           getActivity().setRequestedOrientation(
                 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    }
    

    Then extend this LandscapeFragment from any of your fragment to which you want to launch it in landscape mode.

提交回复
热议问题