1. 程式人生 > >Android 切換全屏,取消全屏

Android 切換全屏,取消全屏

切換全屏,取消全屏

/**
 * 切換全屏,取消全屏
 *
 * @param isChecked
 */
private void switchFullScreen(boolean isChecked) {
    if (isChecked) {
        //切換到全屏模式
        //新增一個全屏的標記
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //請求橫屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        //設定視訊播放控制元件的佈局的高度是match_parent
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mVideoRootView.getLayoutParams();
        //將預設的高度快取下來
        mVideoHeight = layoutParams.height;
        layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
        mVideoRootView.setLayoutParams(layoutParams);
    } else {
        //切換到預設模式
        //清除全屏標記
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //請求縱屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //設定視訊播放控制元件的佈局的高度是200
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mVideoRootView.getLayoutParams();
        layoutParams.height = mVideoHeight;  //這裡的單位是px
        mVideoRootView.setLayoutParams(layoutParams);
    }
}

橫豎屏切換時的生命週期總結:

1、不設定Activity的android:configChanges時,切屏會重新呼叫各個生命週期,切橫屏時會執行一次,切豎屏時會執行兩次

2、設定Activity的android:configChanges=”orientation”時,切屏還是會重新呼叫各個生命週期,切橫、豎屏時只會執行一次

3、設定Activity的android:configChanges=”orientation|keyboardHidden”時,切屏不會重新呼叫各個生命週期,只會執行onConfigurationChanged方