1. 程式人生 > >ActionBarCompat (Part 1): How to use

ActionBarCompat (Part 1): How to use

Google has released a new version of its Support Library and it finally includes ActionBarCompat. We don’t know if ActionBarSherlock days of glory are over, but it’s true that the Android team has been working on ActionBarCompat so hard that it deserves at least one chance.

Coding ActionBarCompat

So let’s create a new project using API 18 and add the project under sdk\extras\android\support\v7\appcompat

folder. I will set light theme with dark Action Bar. It is as easy as usual:

<application
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">

Now your activity needs to extend ActionBarActivity

instead. This activity is based on FragmentActivity, so you will be able to use fragments without any extra effort. It’s easy:

public class MainActivity extends ActionBarActivity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

And here it is!

Action Bar Compat

But what if we want to add some menu items? It’s pretty much the same, but some attributes require our custom namespace:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      >
    <item
        android:id="@+id/action_refresh"
        android:title="@string/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_search"
        android:title="@string/action_search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

That’s the case of showAsAction, or actionViewClass, which will be explained in next episode. Not too difficult, right?

Now you can inflate the menu:

Want to learn Kotlin?

Check my free guide to create your first project in 15 minutes!

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

actionbarcompat2

If you need to access Action Bar programatically, you will call getSupportActionBar()

Conclusion

Using ActionBarCompat is almost as easy as native bar, and even similar to ActionBarSherlock, but there are a few differences that must be know. This first tutorial covered the most simple integration. You can find full code on Github.

In next episodes, I will explain how to include an action view, change action mode or even integrate Navigation Drawer. So please, stay tuned!

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.

Shares

Like this:

Like Loading...

相關推薦

ActionBarCompat (Part 1): How to use

Google has released a new version of its Support Library and it finally includes ActionBarCompat. We don’t know if ActionBarSherlock days of glory ar

[Tutorial, Part 1] How to develop Go gRPC microservice with HTTP/REST endpoint, middleware…

[Tutorial, Part 1] How to develop Go gRPC microservice with HTTP/REST endpoint, middleware, Kubernetes deployment, etc.There are a lot of article how to cr

How to use Context with Dialogflow to handle errors (Part 2: Knock Knock, It’s me)

How to use Contextual Fallback with Dialogflow to handle errors (Part 2: Knock Knock Jokes)(This is Part 2 of a four-part series on how to use Context with

How to use this image - Redis

art compile clu contain ext nal nds iat pop link - https://store.docker.com/images/redis?tab=description start a redis instance $ docker

how to use seeta face engine to detect and recognize face

obb ref mcs oci vdc face gin engine http R啦2Z娜辟絲5卸JZ戮諳http://www.docin.com/app/user/userinfo?userid=179005792 3Z煙1VLBR1吐http://www.docin

[RxJS] Learn How To Use RxJS 5.5 Beta 2

toarray return ray erro bsp err source val com The main changes is about how you import rxjs opreators from now on. And introduce lettabl

Learn how to use Latex.

dex with ocs have been info relation ani tails I had been learning Latex for a little while during my studying at Edinburgh, which was ma

build your own website 1 -- how to install Apache Server

tps href root res apache2 sys targe serve name Operating System: Ubuntu 16.04 Web Server ? most often used Web Server software is freely

How to use Qt Designed Ui file

code ace RM pre pos creat -m pub clu Ui Designed file In Working, we can use Qt Designer to designe UI; Then, use uic -o head.h designe.

How to use Lonsdor K518ISE Hex Editor

K518ISE Key Programmer Lonsdor K518ISE Lonsdor K518ISE Key Programmer Lonsdor K518ISE tablet Lonsdor K518ise update Lonsdor K518ISE Hex Edi

[Selenium+Java] How to use IntelliJ IDEA & Selenium Webdriver

sts quick public das nes www. button margin state Original URL: https://www.guru99.com/intellij-selenium-webdriver.html How to use Int

How to use GITHUB to do source control

GITHUB sourcecontrolhow to create repository how to create branch how to add the comment for every change what is the commit how to rollback how to sync th

How to use Kata Containers and CRI (containerd plugin) with Kubernetes

bsp use k8s doc ner blob ber uber net https://github.com/kata-containers/documentation/blob/master/how-to/how-to-use-k8s-with-cri-contain

How to use Toyota simulated card + P001 Programmer with Obdstar X300 DP PLUS

Obdstar X300 DP Plus OBDSTAR X300 DP Plus key programmer OBDSTAR X300 DP plus PAD X300 DP Plus Purpose: To check if OBDSTAR X300 DP PLUS can

How to use CAR FANS C800 Diagnostic Scan Tool to do diagnosis operation

ces person required pro tel fail del data per How to use Heavy Duty Diagnostic CAR FANS C800 Diagnostic Scan Tool to do diagnosis opera

How to Use vcpkg On Windows

Introduction If you do any sort of C++ development on Windows, then you know that library/package management can be quite a pain at times (ever built Open

logstash output ES, use join ( how to use parent)

https://discuss.elastic.co/t/how-to-create-a-parent-using-the-new-join-type-with-logstash/119523?u=bulu First setup your mapping to create the joi

how to use the volatile keyword keil51 ?

       1. 例如對外部暫存器的讀寫。         對有些外部裝置的暫存器來說,讀寫操作可能都會引發一定硬體操作,但是如果不加volatile,編譯器會把這

如何使用KdTree進行搜尋(How to use a KdTree to search)

在本教程中,我們將詳細介紹如何使用KdTree來查詢特定點或位置的K個最近鄰居,然後我們將繼續介紹如何在使用者指定的半徑範圍內找到所有鄰居(在本例中為隨機) 。 #理論引入 kd樹或k維樹是電腦科學中用於在具有k維的空間中組織若干點的資料結構。這是一個二叉搜尋樹,其他約束條件是強加給