简体   繁体   English

在片段或活动中使用工具栏

[英]Using Toolbar in Fragment or in Activity

I am using Android-ObservableScrollView library. 我正在使用Android-ObservableScrollView库。 Everything works great, but I have activity for holding fragments, so all views are encapsulated in the fragment. 一切都很好,但我有活动来保存片段,所以所有视图都封装在片段中。 In activity there is only FrameLayout for holding fragments. 在活动中,只有FrameLayout用于保存片段。
So I need to use Toolbar in my application, I have several ideas how to implement this. 所以我需要在我的应用程序中使用Toolbar ,我有几个想法如何实现它。

  1. Use Toolbar in activity, in this case my layout will have FrameLayout and Toolbar. 在活动中使用工具栏,在这种情况下,我的布局将包含FrameLayout和Toolbar。 In this way I have communicate with activity whenever I need to do something with toolbar, I can also obtain it by using getSupportedActionBar() from fragment. 通过这种方式,每当我需要对工具栏执行某些操作时,我都会与活动进行通信,我也可以通过使用片段中的getSupportedActionBar()来获取它。

  2. Use Toolbar inside fragment (in its layout) setting in each fragment view creation. 在每个片段视图创建中使用工具栏内部片段(在其布局中)设置。 And each time I change fragment I have to add new Toolbar to the activity. 每次我更改片段时,我都必须在活动中添加新的工具栏。 In some fragment I am going to have different toolbars but not in all. 在某些片段中,我将使用不同的工具栏但不是全部。 Is it good approach to store Toolbar inside fragment. 是否在片段中存储工具栏是一种很好的方法。

The problem that I can see in using second approach, if there will be more than one fragment on the screen there will be also several toolbars. 我在使用第二种方法时可以看到的问题,如果屏幕上会有多个片段,那么还会有几个工具栏。

Please suggest what will be the right way in this case. 请在这种情况下建议正确的方法。
Thank you. 谢谢。

You should use first method. 你应该使用第一种方法。 While using first method will have less problem then second one because in second method you have to define toolbar for many times which is not good programming. 虽然使用第一种方法比第二种方法有更少的问题,因为在第二种方法中你必须多次定义工具栏,这不是很好的编程。

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

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