隐藏片段中的状态栏

问题描述 投票:1回答:3

我试图在片段中隐藏状态栏。我已成功隐藏工具栏,但我想隐藏状态栏也只在一个片段中..

  @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(已在上面提到)应用程序崩溃。

实施第二个答案后增加了三张照片

[正常画面] [1] [使用你的代码作品后] [2] [从全屏幕回来时] [3]

  [1]: https://i.stack.imgur.com/KbX4T.jpg
  [2]: https://i.stack.imgur.com/sSWqf.jpg
  [3]: https://i.stack.imgur.com/urJaK.jpg
android android-fragments android-toolbar android-titlebar
3个回答
0
投票

@Lalit Singh Fauzdar回答隐藏statusBar是正确的。如果你想显示statusBar你可以使用View decorView = getActivity().getWindow().getDecorView(); // Hide the status bar. int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(uiOptions);


4
投票

只是从我的项目中复制粘贴,但这样你可以隐藏片段中的状态栏。

 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类中复制,而不是在任何方法中复制。

是的,您可以只使用以下代码:

getActivity().getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_FULLSCREEN);

0
投票

我希望你这个代码能帮助你更多..

来自活动......

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();
    }
}
 }
© www.soinside.com 2019 - 2024. All rights reserved.