How to create Android ActionBar with custom view and tabs

前端 未结 1 1722
时光说笑
时光说笑 2020-12-17 04:02

I want to create tabbed ActionBar (NAVIGATION_MODE_TABS) with custom view (DISPLAY_SHOW_CUSTOM). It should looks like this:

相关标签:
1条回答
  • 2020-12-17 04:39

    The buttons in the top row have to be added as optionMenu items:

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        for (int i = 0; i < menuItems; i++)
        {
            if (i == 0)
            {
                menu.add(R.string.home).setIcon(R.drawable.button_home).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
                menu.getItem(0).setOnMenuItemClickListener(this);
    
            }
        }
    
        return super.onCreateOptionsMenu(menu);
    }
    

    The philosophy of the ActionBar is slightly different then I supposed to be when asking. From the Android developer guide:

    When you want to provide navigation tabs in an activity, using the action bar's tabs is a great option (instead of using TabWidget), because the system adapts the action bar tabs for different screen sizes—placing them in the main action bar when the screen is sufficiently wide, or in a separate bar (known as the "stacked action bar") when the screen is too narrow.

    0 讨论(0)
提交回复
热议问题