Java android在特定片段中隐藏工具栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29128162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:24:36  来源:igfitidea点击:

android hide toolbar in specific fragment

javaandroidandroid-fragmentstoolbar

提问by smovie9

I have a problem that I don't know how to solve. How do you hide a toolbar in a specific fragment, I have already been searching around on the internet and what I found was communicating activity and fragment would solve it. But it doesn't work for me at all, here is my code:

我有一个问题,我不知道如何解决。你如何在特定片段中隐藏工具栏,我已经在互联网上搜索过,我发现是交流活动,片段可以解决它。但这对我根本不起作用,这是我的代码:

main_activity:

主要活动:

public class MainActivity extends ActionBarActivity implements like_frag.OnHideToolbar{

....

public void onHidingToolbar(int position){
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        if(toolbar == null){
            getSupportActionBar().hide();
        }else{
            getSupportActionBar().hide();
        }
    }

like_frag.java

like_frag.java

public class like_frag extends Fragment {

    OnHideToolbar mCallback;
    Toolbar toolbar;

    public interface OnHideToolbar {
        public void onHidingToolbar(int position);
    }

    public void onAttach(Activity activity){

        try{
            mCallback = (OnHideToolbar) activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString() + "error implementing");
        }
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.swipefrag, container, false);

        toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);

        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }
}

thanks in advance.

提前致谢。

I have a drawer inside the toolbar.

我在工具栏内有一个抽屉。

回答by dc-aoxn

In the fragment's onCreatemethod call:
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
Replace AppCompateActivitywith the activity class you used.

在片段的onCreate方法调用中:
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
替换AppCompateActivity为您使用的活动类。

Edited:

编辑

You could simply use the onResumemethod to call hide()and the onStopmethod to call show()as suggested in some of the comments.

您可以按照某些评论中的建议简单地使用onResume要调用hide()onStop方法和要调用的方法show()

@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}

@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}

回答by Chris Read

Create an interface in the fragment and use it to tell the parent activity to hide the toolbar.

在片段中创建一个界面并使用它来告诉父活动隐藏工具栏。

Add these lines to your fragment:

将这些行添加到您的片段中:

private OnEventListener listener;

public interface OnEventListener {

    void hideToolbar() ;
}

public void setOnEventListener(OnEventListener listener) {

    this.listener = listener;
}

After creating your fragment in the main activity add:

在主活动中创建片段后添加:

    myFragment.setOnEventListener(new MyFragment.OnEventListener() {
        @Override
        public void hideToolbar() {

            getSupportActionBar().hide();
        }
    });

Whenever you need to hide the toolbar execute:

每当您需要隐藏工具栏时,请执行:

listener.hideToolbar();

from inside your fragment.

从你的片段内部。

回答by Soni Kumar

Put this code in fragment in which you want to hide toolbar...

将此代码放在要隐藏工具栏的片段中...

 @Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}

回答by Kaustubh Bhagwat

Just add these methods to the fragment where you want to diable the toolbar ,and also in the fragment's onStop()make it visible again.

只需将这些方法添加到要禁用工具栏的片段中,并在片段中onStop()使其再次可见。

 @Override
    public void onResume() {
        super.onResume();
        ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
    }

    @Override
    public void onStop() {
        super.onStop();
        ((AppCompatActivity)getActivity()).getSupportActionBar().show();
    }

回答by karan1.singh

Put this code in fragment in which you want to hide toolbar...

将此代码放在要隐藏工具栏的片段中...

Add this( ((AppCompatActivity)getActivity()).getSupportActionBar().hide();) in onCreateView or in onResume.

在 onCreateView 或 onResume 中添加 this( ((AppCompatActivity)getActivity()).getSupportActionBar().hide();)。

and do this in onDestroy()

并在 onDestroy() 中执行此操作

@Override
public void onDestroy() {
super.onDestroy();
((AppCompatActivity)getActivity()).getSupportActionBar().show();}

回答by Sanam Yavarpor

in kotlin hide and show supportActionBaras follows:

在 kotlin 中隐藏和显示supportActionBar如下:

override fun onResume() {
    super.onResume()
    (activity as AppCompatActivity).supportActionBar?.hide()
}

override fun onStop() {
    super.onStop()
    (activity as AppCompatActivity).supportActionBar?.show()
}

and if you want to have your own custom toolbar, in OncreateViewset:

如果您想拥有自己的自定义工具栏,请OncreateView设置:

//your Custom toolbar in xml
val toolbar = binding.toolbar
(activity as AppCompatActivity).setSupportActionBar(toolbar)

回答by Sanjeev

If you are using the new Navigation Component, add this while setting up the toolbar

如果您使用的是新的Navigation Component,请在设置工具栏时添加

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
   @Override
   public void onDestinationChanged(@NonNull NavController controller,
           @NonNull NavDestination destination, @Nullable Bundle arguments) {
       if(destination.getId() == R.id.full_screen_destination) {
           toolbar.setVisibility(View.GONE);
           bottomNavigationView.setVisibility(View.GONE);
       } else {
           toolbar.setVisibility(View.VISIBLE);
           bottomNavigationView.setVisibility(View.VISIBLE);
       }
   }
});