Copy ActionBar Tabs with Custom View

I would like to have an ActionBar with custom navigation, where custom views look like standard action bar tabs. I know this sounds like reinventing the wheel, but that means we can have the menu button on the same line as the tabs, as shown below. This is a design requirement and practically does a lot more user interface for this application than the standard Android behavior. How I would like the tabs to look

I tried using IcsLinearLayout from ActionBarSherlock like this:

<IcsLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:orientation="horizontal"
          android:layout_height="50dip">
         <Button
             android:id="@+id/tab_1"
             android:layout_height="match_parent"
             android:gravity="center"
             android:layout_width="wrap_content"
             android:textStyle="bold"
             android:text="TAB_1"
             android:background="@drawable/abs__item_background_holo_light"
             />
        <Button
            android:id="@+id/tab_2"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:textStyle="bold"
            android:text="TAB_2"
            android:background="@drawable/abs__item_background_holo_light"
             />
</IcsLinearLayout>

But that repeats ActionButtons, and I have no idea how to replicate tabs.

I guess I will need:

  • special container viewing group (possibly from the ActionBarSherlock Library)
  • .
  • , , ( RadioButton).

( ActionBarSherlock).

+5
2

//

//pre-ICS
if (actionBarSherlock instanceof ActionBarImpl) {
    enableEmbeddedTabs(actionBarSherlock);

//ICS and forward
} else if (actionBarSherlock instanceof ActionBarWrapper) {
    try {
        Field actionBarField = actionBarSherlock.getClass().getDeclaredField("mActionBar");
        actionBarField.setAccessible(true);
        enableEmbeddedTabs(actionBarField.get(actionBarSherlock));
    } catch (Exception e) {
        Log.e(TAG, "Error enabling embedded tabs", e);
    }
}

//helper method
private void enableEmbeddedTabs(Object actionBar) {
    try {
        Method setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        setHasEmbeddedTabsMethod.setAccessible(true);
        setHasEmbeddedTabsMethod.invoke(actionBar, true);
    } catch (Exception e) {
        Log.e(TAG, "Error marking actionbar embedded", e);
    }
}

. . - https://groups.google.com/forum/#!topic/actionbarsherlock/hmmB1JqDeCk

+13

, , , . , . ScrollingTabContainerView ABS, .

How it all looks with tabs on a narrow action bar

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

    ScrollingTabContainerView root = new ScrollingTabContainerView(this);
    ActionBar.Tab tab1 = getSupportActionBar().newTab();
    tab1.setText("TAB 1");

    tab1.setTabListener(this);

    ActionBar.Tab tab2 = getSupportActionBar().newTab();
    tab2.setText("TAB 2");
    tab2.setTabListener(this);

    root.addTab(tab1, 0, true);
    root.addTab(tab2, 1, false);

    getSupportActionBar().setCustomView(root);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setTitle("App Title");

}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    menu.add("MENU ITEM 1");
    menu.add("MENU ITEM 2");
    return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

, -.

+5

All Articles