replicate ActionBar Tab(s) with custom view

前端 未结 2 1682
北海茫月
北海茫月 2020-12-05 16:51

I would like to have an ActionBar with custom navigation where the custom views look like the standard action bar tabs. I know it sounds like reinventing the wheel but it me

2条回答
  •  醉酒成梦
    2020-12-05 17:09

    By using hierarchy viewer I think we've worked out how to do this. It turns out it wasn't difficult at all. You need a ScrollingTabContainerView from the ABS library and you can add tabs to that.

    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.
    }
    

    I hope this helps someone.

提交回复
热议问题