Action Bar in API Level > 3.0 but Still Support AP

2019-05-31 14:01发布

问题:

Yet another compatibility question.

THE PROBLEM

I need to use a tabbed action bar in api level 3.0 and greater to switch between fragments. However, I also need to be able to switch between these fragments somehow in api level < 3.0.

The spanner in the works is the fact that I have already downloaded a custom compatibility library that allows me to use google maps with fragments and therefore I can't use a library.

THE QUESTION

How can I implement a tabbed ActionBar solution in 3.0 and greater and also cater for the bigger market that is 3.0 and less?

Any help would be grand.

回答1:

What you want is ActionBarSherlock. It uses the native action bar for API >= 3 and provides backwards compatibility for API levels 2.x. There's also the Action Bar Compatibility sample project (listed as SupportAppNavigation, I believe). I think this is essentially the same thing.



回答2:

So I managed to figure a solution a while ago, it's just taken me a while to post it here.

This is the solution that works for me. I needed to use an ActionBar but I also needed to use the MapActivity as well as the FragmentActivity hence not being able to use ActionBarSherlock.

What I did was the following:

  • Created a fragment called CCActionBar which handles the touching of the CCTab's by adding itself as a listener(explained below). It also inflates a layout called action barlayout which has the tabs and images arranged, just reference the tabs at run time.

  • Created a custom view called CCTab which represent the tabs of the action bar. When its touched it tells its listener (CCActionBar) its been touched.

  • In my main layout xml file have an action bar container which I show or hide at runtime depending on the API level.

  • Now in my main activity in my OnCreate method I check whether my API level is greater than 3.0 or not. If it is I just implement the standard ActionBar making sure my custom action bar container is invisible. If it isn't I set up my custom action bar and make sure my action bar container in my xml layout is visible.

    • Then I make sure that when a tab is pressed whilst using either method it is handled in the same way so once its setup you don't have to handle it any differently.

I hope this helps someone somewhere or at least give you an idea of how to proceed.



回答3:

What I would recommend is to use ActionBarSherlock to get the tabbed action bar functionality.

I assume that you also need a MapView support in Fragment. For this I would recommend solution from MapView in a Fragment (Honeycomb) (look at user1414726 answer with sample code).

I think it is a better solution than using library though it is using deprecated LocalActivityManger. But in my opinion it is a better idea than using library where every Activity is a subclass of MapViewActivity which I assume you are using.