android隐藏特定片段中的工具栏 [英] android hide toolbar in specific fragment

查看:99
本文介绍了android隐藏特定片段中的工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不知道如何解决的问题.您如何将工具栏隐藏在特定的片段中,我已经在Internet上四处搜寻,发现交流活动和片段可以解决该问题.但这对我根本不起作用,这是我的代码:

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:

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);
    }
}

提前谢谢.

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

I have a drawer inside the toolbar.

推荐答案

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

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();
}

这篇关于android隐藏特定片段中的工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆