how to set NAVIGATION_MODE_LIST on Toolbar new app

2019-04-04 15:29发布

问题:

Now all methods related to navigation modes in the ActionBar class, such as setNavigationMode()... are now deprecated.

The documentation explains:

Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead.

In my current application, there is a spinner on ActionBar. How do I apply NAVIGATION_MODE_LIST on the new widget Toolbar in the new version appcompat v7 21.
Thanks in advance.

回答1:

With the API 21 the method setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) is deprecated.

The best way to work with a spinner is to use a Toolbar like this:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_actionbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary">

    <Spinner
            android:id="@+id/spinner_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</android.support.v7.widget.Toolbar>

You can find an example in the Google IO 2014



回答2:

As usual Gabriele is right, so your code will look like that:

So It will look like something like that:

    private ActionBar actionBar;
    private Toolbar toolbar;
    private Spinner spinner;
    private List<String> mNavigationItems;
    private ArrayAdapter<CharSequence> mArrayAdapter;
    /***
     * Boolean to know which version is running
     */
    private boolean postICS,postLollipop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_list);//find the toolbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        postLollipop =getResources().getBoolean(R.bool.postLollipop);
        if(postLollipop){
            toolbar.setElevation(15);
        }
        //define the toolbar as the ActionBar
        setSupportActionBar(toolbar);
        actionBar=getSupportActionBar();
        //now manage the spinner
        mNavigationItems=new ArrayList<String>();
        mNavigationItems.add("navigation 1");
        mNavigationItems.add("nav 2");
        mNavigationItems.add("navigation 3");
        spinner= (Spinner) findViewById(R.id.action_bar_spinner);
        mArrayAdapter = new ArrayAdapter(this, R.layout.actionbar_spinner_dropdown_item, mNavigationItems);
        mArrayAdapter.setDropDownViewResource(R.layout.actionbar_spinner_dropdown_item);
        spinner.setAdapter(mArrayAdapter);
    }

And take care not to use spinnerAdapter (because you won't be able to manage its style using the AppTheme).

And if you want to use Tab (NavigationMode.Tabs) you should use now the Design library (explained here:http://android-developers.blogspot.fr/2015/05/android-design-support-library.html?m=1)and copy paste below "

Tabs Switching between different views in your app via tabs is not a new concept to material design and they are equally at home as a top level navigation pattern or for organizing different groupings of content within your app (say, different genres of music).

The Design library’s TabLayout implements both fixed tabs, where the view’s width is divided equally between all of the tabs, as well as scrollable tabs, where the tabs are not a uniform size and can scroll horizontally. Tabs can be added programmatically:

TabLayout tabLayout = ...; tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); However, if you are using a ViewPager for horizontal paging between tabs, you can create tabs directly from your PagerAdapter’s getPageTitle() and then connect the two together using setupWithViewPager(). This ensures that tab selection events update the ViewPager and page changes update the selected tab.

"