2011-11-25 57 views
21

Tôi đang cố tạo nút để tôi có thể ẩn hoặc hiển thị thanh trạng thái trên máy tính bảng của mình.Android: hiển thị/ẩn thanh trạng thái/thanh công suất

Tôi đã đặt trong onCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 

và trong các nút show:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
getWindow().setAttributes(attrs); 

hide:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
getWindow().setAttributes(attrs); 

Bất kỳ gợi ý/Tipps?

// chỉnh sửa

Tôi đã nhìn vào gợi ý này ở đây: http://android.serverbox.ch/?p=306 và thay đổi mã của tôi như thế này:

private void hideStatusBar() throws IOException, InterruptedException { 
    Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"}); 
    proc.waitFor(); 
} 

private void showStatusBar() throws IOException, InterruptedException { 
    Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"}); 
    proc.waitFor(); 
} 

Vì vậy, nếu tôi bấm vào nút của tôi một phương pháp được gọi là tôi có thể thấy điều gì đó đang xảy ra vì ứng dụng đang chờ một vài giây. Tôi cũng nhìn vào LockCat và thấy rằng một cái gì đó đang xảy ra.

show: http://pastebin.com/CidTRSTi hide: http://pastebin.com/iPS6Kgbp

+0

Ví dụ http://stackoverflow.com/a/35886019/4395114 –

Trả lời

78

Bạn có theme toàn màn hình thiết lập trong biểu hiện?

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 

Tôi không nghĩ rằng bạn sẽ có thể sử dụng chế độ toàn màn hình mà không cần điều này.

tôi sẽ sử dụng sau đây để thêm và loại bỏ các lá cờ fullscreen:

// Hide status bar 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
// Show status bar 
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
+0

Không, tôi đã không thêm nó vào manifest.Nhưng khi tôi thử, tôi nhận được lỗi: Lỗi: Không tìm thấy tài nguyên nào khớp với tên đã cho (tại 'chủ đề' có giá trị '@android: style/ Theme.Dark.NoTitleBar.Fullscreen'). " – B770

+1

@ B770: Có không có tên 'Theme.Dark.NoTitleBar.Fullscreen'. Có một tên' Theme.NoTitleBar.Fullscreen' Tuy nhiên, bạn không thể loại bỏ thanh hệ thống trên các thiết bị Android 3.0+ thiếu các nút HOME và BACK ngoài màn hình. – CommonsWare

+0

@CommonWare: Tôi tìm thấy điều này: http://forum.xda-developers.com/showthread.php?t=1265397 tại đây có thể ẩn và hiển thị thanh trạng thái. Tôi đã cài đặt ROM "Vượt qua" nơi funktion này được bao gồm và nó hoạt động.Vì vậy, tôi nghĩ nó cũng sẽ có thể đặt funktion này trên một nút khác ... – B770

25

Đối với một số người, Hiển thị thanh trạng thái bằng cách xóa FLAG_FULLSCREEN có thể không hoạt động,

Đây là giải pháp mà làm việc cho tôi , (Documentation) (Flag Reference)

Hide Status Bar

// Hide Status Bar 
if (Build.VERSION.SDK_INT < 16) { 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} 
else { 
    View decorView = getWindow().getDecorView(); 
    // Hide Status Bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
} 

Hiện Status Bar

if (Build.VERSION.SDK_INT < 16) { 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 
    else { 
     View decorView = getWindow().getDecorView(); 
     // Show Status Bar. 
     int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; 
     decorView.setSystemUiVisibility(uiOptions); 
    } 
-2

Reference - https://developer.android.com/training/system-ui/immersive.html

// This snippet shows the system bars. It does this by removing all the flags 
// except for the ones that make the content appear under the system bars. 
private void showSystemUI() { 
    mDecorView.setSystemUiVisibility(
      View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
} 
2

Một trong những tính năng được giới thiệu trong KitKat là "Immersive Chế độ". Chế độ chìm cho người dùng khả năng hiển thị/ẩn thanh trạng thái và thanh điều hướng bằng thao tác vuốt. Để thử, hãy nhấp vào nút "Chuyển đổi chế độ chìm", sau đó thử vuốt thanh vào và ra!

Ví dụ:

public void toggleHideyBar() { 

     int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility(); 
     int newUiOptions = uiOptions; 
     boolean isImmersiveModeEnabled = 
       ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); 
     if (isImmersiveModeEnabled) { 
      Log.i(TAG, "Turning immersive mode mode off. "); 
     } else { 
      Log.i(TAG, "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; 
     } 

     if (Build.VERSION.SDK_INT >= 18) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 
     } 

     getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions); 
     //END_INCLUDE (set_ui_flags) 
    } 
Các vấn đề liên quan