2011-10-07 64 views
17

Tôi cần một số trợ giúp để chuyển sang chế độ toàn màn hình. Tôi có một cài đặt trong màn hình ưu tiên để chuyển sang chế độ toàn màn hình. Trong hoạt động chính của tôi onResume tôi có:Chuyển đổi chế độ toàn màn hình

if(mFullscreen == true) { 
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

      } else 
      { 
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

      } 

Nhưng điều này dường như không hoạt động vì nó cần được gọi trước setContentView phải không?

... Nhưng Ngoài ra, tôi có requestWindowFeature(Window.FEATURE_NO_TITLE); trước setContentView và nó lấy đi tiêu đề và thanh trạng thái ... Ai có thể cung cấp trợ giúp nào không?

--- Chỉnh sửa --- Ok, tôi đã xảy ra lỗi khiến việc này không hoạt động. Vì vậy, nó thực sự làm. Bây giờ, tôi chỉ cần biết làm thế nào để chuyển đổi thanh tiêu đề.

Trả lời

22
private void setFullscreen(boolean fullscreen) 
{ 
    WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
    if (fullscreen) 
    { 
     attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    } 
    else 
    { 
     attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    } 
    getWindow().setAttributes(attrs); 
} 
3

Giải pháp của tôi kết hợp câu trả lời từ: Câu trả lời

tôi đã thêm các phương pháp này để Hoạt động của tôi. Để chuyển đổi toàn màn hình, hãy sử dụng setFullScreen(!isFullScreen()).

public boolean isFullScreen() { 

    return (getWindow().getAttributes().flags & 
     WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0; 
} 

@SuppressLint("NewApi") 
public void setFullScreen(boolean full) { 

    if (full == isFullScreen()) { 
     return; 
    } 

    Window window = getWindow(); 
    if (full) { 
     window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } else { 
     window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 

    if (Build.VERSION.SDK_INT >= 11) { 
     if (full) { 
      getActionBar().hide(); 
     } else { 
      getActionBar().show(); 
     } 
    } 
} 

Trong trường hợp của tôi, tôi muốn có nút menu để chuyển đổi. Vấn đề: trên thiết bị không có nút menu phần cứng, ẩn thanh tác vụ cũng ẩn nút chuyển để quay lại từ chế độ toàn màn hình. Vì vậy, tôi đã thêm một số logic bổ sung để nó chỉ ẩn thanh hành động nếu thiết bị có nút menu phần cứng. Lưu ý rằng các thiết bị chạy SDK 11-13 không có.

if (Build.VERSION.SDK_INT >= 14 
     && ViewConfiguration.get(this).hasPermanentMenuKey()))) { 

     if (full) { 
      getActionBar().hide(); 
     } else { 
      getActionBar().show(); 
     } 
    } 

Các thiết bị cũ hơn (chạy Gingerbread hoặc cũ hơn) có Thanh tiêu đề thay vì Thanh tác vụ. Đoạn mã sau sẽ ẩn nó, nhưng lưu ý rằng Thanh Tiêu đề không thể được hiển thị/ẩn sau khi hoạt động đã bắt đầu. Tôi đã gửi thông báo cho người dùng trong trình đơn trợ giúp của tôi cho biết rằng các thay đổi trên toàn màn hình có thể không có hiệu lực đầy đủ trên các thiết bị cũ cho đến khi họ khởi động lại ứng dụng/hoạt động (tất nhiên giả sử bạn vẫn chọn lựa và thực thi mã này nếu họ muốn) toàn màn hình).

// call before setContentView() 
    if (Build.VERSION.SDK_INT < 11) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
    } 
6

Vì Jellybean (4.1) có một phương pháp mới không phụ thuộc vào Trình quản lý cửa sổ. Thay vì sử dụng setSystemUiVisibility off của Window, điều này cho phép bạn kiểm soát chi tiết hơn trên các thanh hệ thống hơn là sử dụng các cờ của WindowManager. Đây là cách bạn cho phép toàn màn hình:

if (Build.VERSION.SDK_INT < 16) { //ye olde method 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} else { // Jellybean and up, new hotness 
    View decorView = getWindow().getDecorView(); 
    // Hide the status bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
    // Remember that you should never show the action bar if the 
    // status bar is hidden, so hide that too if necessary. 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 

Và đây là cách bạn phục hồi các mã trên:

if (Build.VERSION.SDK_INT < 16) { //ye olde method 
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} else { // Jellybean and up, new hotness 
    View decorView = getWindow().getDecorView(); 
    // Hide the status bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; 
    decorView.setSystemUiVisibility(uiOptions); 
    // Remember that you should never show the action bar if the 
    // status bar is hidden, so hide that too if necessary. 
    ActionBar actionBar = getActionBar(); 
    actionBar.show(); 
} 
+0

Lưu ý rằng bạn có thể cần phải gọi 'getSupportActionBar()' để thay thế. – JakeSteam

+0

Tôi sử dụng phương pháp này , trên thanh trạng thái máy huawei đã ẩn nhưng chế độ xem không thể tự động điều chỉnh vị trí ở đầu màn hình – Carl

3

Có một ngắn hơn Toggle đầy đủ phương pháp màn hình thực hiện:

private void toggleFullscreen() { 
    WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    getWindow().setAttributes(attrs); 
} 

Nó sử dụng Bitwise XOR logic để chuyển đổi FLAG_FULLSCREEN.

8
/** 
* toggles fullscreen mode 
* <br/> 
* REQUIRE: android:configChanges="orientation|screenSize" 
* <pre> 
* sample: 
*  private boolean fullscreen; 
*  ................ 
*  Activity activity = (Activity)context; 
*  toggleFullscreen(activity, !fullscreen); 
*  fullscreen = !fullscreen; 
* </pre> 
*/ 
private void toggleFullscreen(Activity activity, boolean fullscreen) { 
    if (Build.VERSION.SDK_INT >= 11) { 
     // The UI options currently enabled are represented by a bitfield. 
     // getSystemUiVisibility() gives us that bitfield. 
     int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility(); 
     int newUiOptions = uiOptions; 
     boolean isImmersiveModeEnabled = 
       ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); 
     if (isImmersiveModeEnabled) { 
      Log.i(context.getPackageName(), "Turning immersive mode mode off. "); 
     } else { 
      Log.i(context.getPackageName(), "Turning immersive mode mode on."); 
     } 

     // Navigation bar hiding: Backwards compatible to ICS. 
     if (Build.VERSION.SDK_INT >= 14) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 
     } 

     // Status bar hiding: Backwards compatible to Jellybean 
     if (Build.VERSION.SDK_INT >= 16) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN; 
     } 

     // Immersive mode: Backward compatible to KitKat. 
     // Note that this flag doesn't do anything by itself, it only augments the behavior 
     // of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample 
     // all three flags are being toggled together. 
     // Note that there are two immersive mode UI flags, one of which is referred to as "sticky". 
     // Sticky immersive mode differs in that it makes the navigation and status bars 
     // semi-transparent, and the UI flag does not get cleared when the user interacts with 
     // the screen. 
     if (Build.VERSION.SDK_INT >= 18) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 
     } 
     activity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions); 
    } else { 
     // for android pre 11 
     WindowManager.LayoutParams attrs = activity.getWindow().getAttributes(); 
     if (fullscreen) { 
      attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
     } else { 
      attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
     } 
     activity.getWindow().setAttributes(attrs); 
    } 

    try { 
     // hide actionbar 
     if (activity instanceof ActionBarActivity) { 
      if (fullscreen) ((ActionBarActivity) activity).getSupportActionBar().hide(); 
      else ((ActionBarActivity) activity).getSupportActionBar().show(); 
     } else if (Build.VERSION.SDK_INT >= 11) { 
      if (fullscreen) activity.getActionBar().hide(); 
      else activity.getActionBar().show(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // set landscape 
    // if(fullscreen) activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
    // else activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
} 

mã của tôi hoạt động tốt với Android 2.3 và 4.4.2

+2

Phương pháp cập nhật tuyệt vời! Hai điều mặc dù: 1) sửa đổi newUiOptions thiếu các phần "& = ~ xxx" không toàn màn hình, 2) lib ứng dụng mới nhất sử dụng AppCompatActivity thay vì ActionBarActivity. –

+0

cảm ơn lớn đã làm việc cho tôi – Richi

Các vấn đề liên quan