Use ActionBar compability version with Android 4.0

2019-09-05 13:12发布

问题:

I followed the code in the ActionBar compability version

When I use the code for API level 10 or less than it, it shows a ActionBar, but when I use it for API level 11 or greater, It does not show logo icon and Action items.

I checked the classes and I understood:

ActionBarHelperBase : API level 10 or less.

ActionBarHelperHoneycomb : API level 11 or greater.

ActionBarHelperICS : API level 14 or greater.

What is the best way to fix the ActionBar for Android Version 3.0 or greater? Specially Android 4.0

回答1:

The problem was in the Manifest file. I should remove android:theme="@style/Theme.ThemeCompabilityVersion from the Manifest file.

Then I need to check the version in the code in OnCreate() method like:

private void setTheme() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) 
        setTheme(R.style.ThemeCompabilityVersion);
    else 
        setTheme(R.style.AppTheme);         
}


回答2:

There is an incredible project called ActionBarSherlock that allows to use all the ActionBar functionality in API over 2.X

It will took you a sample project to understand and set the dependencies and it will solve a lot of your problems :) just call getSupportActionBar() and all methods will be available.

Some common errors that i made was to not setting the Theme correctly.

<style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="background">@drawable/bg_striped</item>
<item name="android:background">@drawable/bg_striped</item>

<item name="backgroundSplit">@drawable/bg_striped_split</item>
<item name="android:backgroundSplit">@drawable/bg_striped_split</item>

Then you have to apply that Theme to each activity that will have ActionBar support or to the application tag.

<application android:icon="@drawable/icon" 
             android:label="@string/app_name" 
             android:name=".ApplicationClass"
             android:theme="@style/Theme.Styled">

Also you can set a menu.xml containing the items on the Bar:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_save"
      android:icon="@drawable/ic_launcher"
      android:title="TestMe"
      android:menuCategory="system"
      android:showAsAction="never" />
<item android:id="@+id/menu_launch"
      android:icon="@drawable/ic_launcher"
      android:title="Test2"
      android:menuCategory="secondary"
      android:showAsAction="never" />
<item android:id="@+id/menu_go"
      android:icon="@drawable/ic_launcher"
      android:title="Test"
      android:showAsAction="always|withText" />
</menu>

The most interesting thing that i learned is that each activity or fragment can put their actions on the bar, so if a fragment is searchable an option with magnifying glass icon can be set, etc.

It had some problems with fragments in a prior version, in current version seems to be solved, i can put some example code to set it up with RoboGuice and fragment support if needed.

Hope it helps, Regards.



回答3:

ActionbarSherlock is higly recommended, a much more complete and advanced compability version. It uses the native actionbar if running on an android version having one etc of course.

actionbarsherlock.com



回答4:

Have you set your targetSdkVersion to 11 or higher? You can keep a lower minSdkVersion, for example:

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />