Android我们可以在调用onAttachedToWindow()时隐藏状态栏吗?

时间:2011-12-21 18:24:46

标签: java android status statusbar

当调用onAttachedToWindow并且窗口类型是keyguard时,有一种隐藏状态栏或禁用它的方法吗? 我在我的活动课上试过这个:

这是onCreate

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setWindowAnimations(0);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_lock); 
}

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

这在我的清单

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

但有时状态栏会消失,有时会显示。

如果我试试这个:

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

它给了我错误:

12-22 11:03:51.209: E/AndroidRuntime(9088): android.util.AndroidRuntimeException: requestFeature() must be called before adding content

1 个答案:

答案 0 :(得分:0)

好的,在您以编程方式隐藏状态栏之前,您必须请求该功能:

requestWindowFeature(Window.FEATURE_NO_TITLE);

然后要请求全屏,您可以使用:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);