简体   繁体   中英

How to add spinner in action bar menu items

在此处输入图片说明 I have searched a-lot about this but every one just changing the title into spinner. I want some thing like if I add menu item and add this in its code.

    <item 
    android:id="@+id/spListOpt"
    android:showAsAction="always"
    android:actionViewClass="android.widget.Spinner"
    android:title="@string/logout"
          />

Saw this on stack over flow but it doesn't show anything on action bar. Attaching picture for better understanding.

You can set custom view for that,

Example:

MenuItem menuItem = menu.findItem(R.id.spListOpt);
menuItemRefresh = MenuItem.setActionView(menuItem,R.layout.customview); 

Reference:

setActionView for MenuItem

example

you can add the spinner to the Actionbar the following way:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Inflate your menu layout
    getMenuInflater().inflate(R.menu.main_activity_menu, menu);
    MenuItem menuItem = menu.findItem(R.id.spListOpt);
    Spinner yourSpinnerName = (Spinner) menuItem.getActionView();
    ArrayList<String> spinnerStringArray = new ArrayList<>();
    //Add your data to your array
    spinnerStringArray.add("ItemOne");

    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, mSpinnerStringArray);
    yourSpinnerName.setAdapter(spinnerAdapter);
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM