Android Official Demo ActionBarCompat-Styled

ActionBarCompat-Styled Demo download address:

This example demonstrates how to use the custom ActionBarCompat, which contains three paging tags.It is important to note that the paging mode of ActionBar has been abandoned since API 21.

The interface may vary from device to device. Here are some screenshots:



Steps to achieve:

  1. Create Activity and inherit AppCompatActivity to implement ActionBar.TabListener interface;
  2. Create a menu folder under the res resource folder and a main menu layout xml under the menu folder.
  3. Override the onCreateOptionsMenu() method to create and initialize menus;
  4. Create and initialize Tab paging.

Below is the Demo code, which allows you to page different content by loading fragments.

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class ActionBarStyledActivity extends AppCompatActivity implements ActionBar.TabListener {

    private final String TAG = "ActionBarStyledActivity";

    protected void onCreate(Bundle savedInstanceState) {
        // Setup Menu Use Tab paging
        ActionBar ab = getSupportActionBar();
        Set the current navigation mode.There are three navigation modes:
         NAVIGATION_MODE_STANDARD: Standard navigation mode.
         NAVIGATION_MODE_LIST: Label navigation mode.
         NAVIGATION_MODE_TABS: Drop-down list navigation mode.

        // Establish Tab paging
        ab.addTab(ab.newTab().setText("Tab 1").setTabListener(this));
        ab.addTab(ab.newTab().setText("Tab 2").setTabListener(this));
        ab.addTab(ab.newTab().setText("Tab 3").setTabListener(this));

    public boolean onCreateOptionsMenu(Menu menu) {
        // Bind menu layout file, which has two menu subitems defined refresh and settings
        getMenuInflater().inflate(, menu);
        return super.onCreateOptionsMenu(menu);

    public void onTabSelected(ActionBar.Tab tab, ft) {
        // Tab Called when paging is selected.
        Log.i(TAG, tab.getText() + " onTabSelected");

    public void onTabUnselected(ActionBar.Tab tab, ft) {
        // current Tab Paging is no longer selected and is called when replaced by the next selected page.For example, currently selected Tab1,When you click Tab2 When, Tab1 This method will be triggered, and Tab2 Will trigger onTabSelected.
        Log.i(TAG, tab.getText() + "onTabUnselected");

    public void onTabReselected(ActionBar.Tab tab, ft) {
        // This is selected Tab Paging, called when you click again to select the same page.For example, it is already selected Tab1,When you click again Tab1 When, Tab1 This method will be triggered.
        Log.i(TAG, tab.getText() + "onTabReselected");

Tags: Android github xml

Posted on Tue, 24 Mar 2020 09:25:01 -0700 by mband2000