Android, reusable toolbar for navigation - where to put code?

て烟熏妆下的殇ゞ 提交于 2019-12-24 12:46:36

问题


I want to implement a toolbar for navigation which appears at the bottom of almost all activities in my app. It will have a fixed amout of elements (four).

Screenshot: (certain activities will still have their own toolbar at the top):

In order to make it somewhat reusable, I've created a separate layout file for the toolbar which allows me to include it on multiple activities with:

<include android:id="@+id/toolbar_main" layout="@layout/toolbar_main" />

Every item of the toolbar leads to another activity, which means it acts as a navigation throughout the whole application.

However, as I want to use it on multiple activities, I'm not sure on what would be the correct place for the Java-Code behind the onClick-Events of the menu items. I've seen approaches using a base-class which can be extended by all activities using this navigation toolbar. I've also thought about not using a toolbar at all and creating a fragment for the navigation. I haven't used fragments yet, however, they seem to have their own code-behind class in addition to their design .xml, which would be suitable for the click-Events. Any suggestions? I might add, that I want to center and stretch the navigation bar later on, which seems to be not so easy using a Toolbar with associated menu.


回答1:


Make A Base activity, which will be extending by all your other activities. Write all your logic related to that toolbar on Base Activity. Thats all.




回答2:


You could use the TabLayout with ViewPager. Place them in main Activity, and use Fragment for every root screen.



来源:https://stackoverflow.com/questions/35942653/android-reusable-toolbar-for-navigation-where-to-put-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!