ActionBar in API 8-10 in Android

2019-04-16 17:31发布

问题:

i want to use ActionBar in API level 8-10. i search solutions and find out 2 links bellow, but they are not useful for me.is there someone who has the same experience?Thanks!

this is my code:

 ActionBar actionBar =getActionBar();
    assert actionBar != null;
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#e32238")));

link1 link2

when i run the proje,these errors are shown:

回答1:

ActionBar added in Android 3.0 (API level 11), but you can use it in ealier API 11 by using Spport Library. To use support library for Action Bar, your activity should extends ActionBarActivity.

To custom your action bar color, you can set actionbar style in styles.xml file, something like:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">                
    <item name="actionBarStyle">@style/MyActionBar</item>

</style>


<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- for before API 11 -->
    <item name="background">@color/your_color</item>
    <!-- for after API 11 -->
    <item name="android:background">@color/bg_blue</item>
</style>

Hope this help!

[UPDATE]

To use AppCompat theme, you have to import AppCompat library to your project from support library V7: your-android-sdk\platforms\extras\android\support\v7\appcompat



回答2:

In one of my projects, I used ActionBarSherlock.

  1. Download the abs here.

  2. Assuming you're using Eclipse IDE, right click your project, click properties, then android. At the bottom, in layout, add abs.

  3. Make sure abs and your project are in the same directory.

  4. Extend your classes to Sherlock naming. For example, instead of Activity, change it to Sherlock Activity. The main documentation of abs is found here.

  5. For easier usage of Sherlock, create util methods in changing the Sherlock names and modifying it.

Here is a sample code.