简体   繁体   English

与片段的导航视图。工具栏

[英]Navigation view with fragment. Toolbar

So, I have an activity with navigation view. 所以,我有一个导航视图的活动。 By click on its item I change fragment in activity. 通过单击其项目我更改活动中的片段。 All fragment have the same toolbar. 所有片段都有相同的工具栏。 But one have this toolbar and TabLayout to it. 但是有一个工具栏和TabLayout。 I would like to know what is better to declare toolbar once on activity like this 我想知道在这样的活动上声明工具栏一次更好

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar" />

</RelativeLayout>

or declare it in each fragment. 或在每个片段中声明它。

The disadvantage of the first method is default toolbar shadow. 第一种方法的缺点是默认工具栏阴影。 When I add tabs in fragment, shadow looks like 当我在片段中添加标签时,阴影看起来像

在此输入图像描述

When I tried 2 solution. 当我尝试2解决方案。 All my toolbar was with back icon instead drawer animated logo. 我的所有工具栏都带有后退图标,而不是抽屉动画徽标。

Thanks. 谢谢。

I had the exact same problem. 我有同样的问题。 This is how I solved it: 这就是我解决它的方式:

  • Move the toolbars to the fragments like you suggested (so you won't have a shadow separating the two). 将工具栏移动到您建议的碎片上(这样您就不会将两者分开)。 This allows for a way more flexible way to implement (different) toolbars in your layouts too. 这样就可以更灵活地在布局中实现(不同的)工具栏。
  • Replace the Toolbar's navigation icon by a custom one like this: 用这样的自定义替换工具栏的导航图标:

     toolbar.setNavigationIcon(R.drawable.ic_action_menu); 

(I used the Android Asset Studio to easily create an icon with the preferred color) (我使用Android Asset Studio轻松创建一个首选颜色的图标)

  • Now open the NavigationView with the new menu(home) icon. 现在使用新菜单(主页)图标打开NavigationView。 You can do this through the MainActivity (the one with the NavigationView). 您可以通过MainActivity(使用NavigationView的那个)执行此操作。 Create a public method in that Activity that opens the drawer: 在该Activity中创建一个打开抽屉的公共方法:

     public void openDrawer(){ mDrawerLayout.openDrawer(Gravity.LEFT); } 
  • Now call this method in the OnOptionsItemSelected in your fragments like this: 现在在片段中的OnOptionsItemSelected中调用此方法,如下所示:

     @Override public boolean onOptionsItemSelected(MenuItem item) { // handle item selection switch (item.getItemId()) { case android.R.id.home: //Menu icon ((MainActivity)getActivity()).openDrawer(); return true; default: return super.onOptionsItemSelected(item); } } 

That's it. 而已。 Of course the downside is that you must implement the Toolbar in each Fragment. 当然缺点是你必须在每个片段中实现工具栏。 However, this is the only way (that I know of) that enables you to have the Toolbar (+TabLayout) in a Fragment and still be able to control your NavigationView. 但是,这是我知道的唯一方法,它允许您在片段中使用工具栏(+ TabLayout)并仍然能够控制您的NavigationView。

You can use AppBarLayout from design support library like: 您可以使用设计支持库中的AppBarLayout,例如:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            ...
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            ...
            />
    </android.support.design.widget.AppBarLayout>

and then you can change visibility of tablayout. 然后你可以改变tablayout的可见性。

For more information about desing layout library : link 有关设计布局库的更多信息: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM