gpt4 book ai didi

Android:检测导航栏可见性

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:21 25 4
gpt4 key购买 nike

如何检测导航栏的存在并隐藏它?

在我的 onCreate() 中,我调用了 hideNavigationBar() 方法来隐藏导航栏,然后我注册了一个监听器,以便在导航栏每次显示时隐藏它根据 documentations 的报告,用户触摸了屏幕上的任何位置.当导航栏在触摸事件后变得可见时,监听器再次调用 hideNavigationBar() 方法,但它没有效果,导航栏仍然可见。

这是我的onCreated() 方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

hideNavigationBar();

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
Toast.makeText(getApplicationContext(), "Visible", Toast.LENGTH_SHORT).show();
hideNavigationBar();
} else {
Toast.makeText(getApplicationContext(), "Not visible", Toast.LENGTH_SHORT).show();
}
}
});
}

这是我的hideNavigationBar() 方法:

 private void hideNavigationBar() {

Toast.makeText(getApplicationContext(), "hideNavigationBar()", Toast.LENGTH_SHORT).show();

View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}

如何在每次可见时隐藏导航栏?

谢谢

最佳答案

您可以将此代码添加到 Activity 的 onCreate() 方法中:

      View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
// TODO: The navigation bar is visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
hideNavigationBar()

} else {
// TODO: The navigation bar is NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});

让您的用户界面与系统栏可见性的变化保持同步通常是一种很好的做法。例如,您可以使用此监听器来隐藏和显示操作栏,以配合状态栏的隐藏和显示。 Android-Responding to UI Visibility Changes

关于Android:检测导航栏可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775694/

25 4 0