(The
Action Bar APIs were first added in Android 3.0 as following picture) 這個範例將教大家,如何在Action bar上加入下拉選單
(This example will teach you how to add a dropdown item on action bar)
File : MainActivity.java
import java.util.ArrayList; import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.app.ActionBar.OnNavigationListener; import android.util.Log; import android.widget.ArrayAdapter; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ActionBar actionBar = getActionBar(); actionBar.setDisplayShowTitleEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); ArrayListitemList = new ArrayList (); itemList.add("WMP"); itemList.add("iTunes"); itemList.add("Winamp"); ArrayAdapter adapt = new ArrayAdapter (this, android.R.layout.simple_list_item_1, android.R.id.text1, itemList); actionBar.setListNavigationCallbacks(adapt, new DropDownListenser()); } class DropDownListenser implements OnNavigationListener { public boolean onNavigationItemSelected(int itemPosition, long itemId) { if (itemPosition == 0) // windows media player { Log.e("log", "you choose WMP"); } if (itemPosition == 1) // iTunes { Log.e("log", "you choose iTunes"); } if (itemPosition == 2) // Winamp { Log.e("log", "you choose Winamp"); } return true; } } }
沒有留言:
張貼留言