Android

Android 動態判斷頁面是否顯示並且旋轉 (Activity內的Fragment也能用)

在Activity&Fragment複寫方法

 @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        //是否顯示在用戶手機上
        if(isVisibleToUser) {
            Activity a = getActivity();
            if(a != null)
            //旋轉成橫屏
            a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            //旋轉成豎屏
            a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

發表迴響