在片段中隐藏状态栏
Hide Statusbar in fragment
我想在片段中隐藏状态栏。
我已经成功隐藏了工具栏,但我也只想在一个片段中隐藏状态栏..
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
((HomeActivity)getActivity()).getSupportActionBar().hide();
}
以上代码可以很好地隐藏工具栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
如果我 requestWindowFeature
在片段 onCreate
(上面提到的)应用程序崩溃了。
在实现@Mathan Chinna 的回答后添加了三个屏幕截图
- 普通屏幕
- 实现@Mathan Chinna 的代码后
- 当我从全屏模式回来时
尽管只是从我的项目中复制粘贴,但这样您就可以在片段中隐藏状态栏。
private int currentApiVersion;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
currentApiVersion = android.os.Build.VERSION.SDK_INT;
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if(currentApiVersion >= Build.VERSION_CODES.KITKAT) {
getActivity().getWindow().getDecorView().setSystemUiVisibility(flags);
final View decorView = getActivity().getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
}
return inflater.inflate(R.layout.yourLayout, container, false);
}
请记住,此代码将被复制到您的 Fragment class 而不是任何方法中。
是的,您可以只使用以下代码:
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_FULLSCREEN);
我希望这段代码对你有更多帮助..
从Activity...
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// YOUR FRAGMENT
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,fragment.toString());
fragmentTransaction.commit();
}
public void HideStatusBar() {
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
public void ShowStatusBar() {
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
来自片段..
public class FragmentOne extends Fragment{
private Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity) getActivity()).HideStatusBar();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = activity;
}
@Override
public void onDetach() {
super.onDetach();
this.mActivity = null;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (mActivity!=null){
((MainActivity) getActivity()).ShowStatusBar();
}
}
}
@Lalit Singh Fauzdar 隐藏状态栏的答案是正确的。如果你想显示状态栏,你可以使用
View decorView = getActivity().getWindow().getDecorView(); // Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(uiOptions);
我想在片段中隐藏状态栏。 我已经成功隐藏了工具栏,但我也只想在一个片段中隐藏状态栏..
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
((HomeActivity)getActivity()).getSupportActionBar().hide();
}
以上代码可以很好地隐藏工具栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
如果我 requestWindowFeature
在片段 onCreate
(上面提到的)应用程序崩溃了。
在实现@Mathan Chinna 的回答后添加了三个屏幕截图
- 普通屏幕
- 实现@Mathan Chinna 的代码后
- 当我从全屏模式回来时
尽管只是从我的项目中复制粘贴,但这样您就可以在片段中隐藏状态栏。
private int currentApiVersion;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
currentApiVersion = android.os.Build.VERSION.SDK_INT;
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if(currentApiVersion >= Build.VERSION_CODES.KITKAT) {
getActivity().getWindow().getDecorView().setSystemUiVisibility(flags);
final View decorView = getActivity().getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
}
return inflater.inflate(R.layout.yourLayout, container, false);
}
请记住,此代码将被复制到您的 Fragment class 而不是任何方法中。
是的,您可以只使用以下代码:
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_FULLSCREEN);
我希望这段代码对你有更多帮助..
从Activity...
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// YOUR FRAGMENT
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,fragment.toString());
fragmentTransaction.commit();
}
public void HideStatusBar() {
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
public void ShowStatusBar() {
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
来自片段..
public class FragmentOne extends Fragment{
private Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity) getActivity()).HideStatusBar();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = activity;
}
@Override
public void onDetach() {
super.onDetach();
this.mActivity = null;
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (mActivity!=null){
((MainActivity) getActivity()).ShowStatusBar();
}
}
}
@Lalit Singh Fauzdar 隐藏状态栏的答案是正确的。如果你想显示状态栏,你可以使用
View decorView = getActivity().getWindow().getDecorView(); // Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(uiOptions);