要在Android Fragment中永久隐藏导航栏并在BottomSheetFragment中打开或始终隐藏它,您可以使用以下解决方案。
首先,在您的Activity中设置以下属性来隐藏导航栏:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 隐藏导航栏
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
接下来,在您的Fragment中,您可以使用以下代码来打开或隐藏导航栏:
public class BottomSheetFragment extends Fragment {
private boolean isNavigationBarVisible = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏导航栏
toggleNavigationBarVisibility(false);
}
@Override
public void onDestroy() {
super.onDestroy();
// 恢复导航栏的可见性
toggleNavigationBarVisibility(true);
}
private void toggleNavigationBarVisibility(boolean isVisible) {
if (isVisible) {
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
} else {
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
}
这样,当您打开BottomSheetFragment时,导航栏将被隐藏。当您离开该Fragment时,导航栏将恢复可见性。
请注意,这种方法仅在Android 4.4(API级别19)及更高版本的设备上有效,因为在较早的Android版本中,系统不支持隐藏导航栏。