问题
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