我可以分别为每个片段设置工具栏吗?如何处理导航抽屉 [英] Can I have toolbar for each fragment separately. How to handle navigation drawer

查看:80
本文介绍了我可以分别为每个片段设置工具栏吗?如何处理导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,某些页面在工具栏中具有自定义视图.有些片段具有透明的工具栏,有些具有坐标布局滚动.

In my app some pages have custom view in toolbar. Some fragment have transparent toolbar and some have coordinate layout scroll.

所以我决定为我想知道的每个片段单独设置工具栏.

So I have decided to have toolbar separate for each fragment I want to know is it a good practice or not.

如果有人已经这样做,请分享代码或示例.

If someone has already done this please share code or example.

推荐答案

您可以在片段中使用自定义工具栏.并且您必须为每个片段分别实现它们.首先在片段布局中声明工具栏:

You can use custom toolbars in your fragments. and you have to implement them separately for each fragment. First of all declare your toolbar in your fragment layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            >
<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:gravity="start"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    >

    <RelativeLayout
       // your custom toolbar layout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>


</android.support.v7.widget.Toolbar>

然后在您的片段中实现它:

Then implement it in your fragment:

在片段中找到它:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle 
    savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);

        //set toolbar appearance
        toolbar.setBackground(your background);

        //for create home button
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

  return view;
}

是的,您可以实现单击侦听器以及您想对工具栏执行的任何操作.有关更多信息,请查看第二个答案: 如何获取自定义工具栏

And yes you can implement click listeners and whatever you want to do with your toolbar. For more take a look at the second answer: How to get custom toolbar

这篇关于我可以分别为每个片段设置工具栏吗?如何处理导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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