ActionBar中的多个微调器

时间:2013-04-14 19:50:23

标签: android spinner android-spinner

我使用ActionBarSherlock,我将导航模式设置为'list'

getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, this);

是否可以动态显示第二个微调器,具体取决于选择的项目?

3 个答案:

答案 0 :(得分:1)

使用自定义操作栏布局。

actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.action_bar_custom);

action_bar_custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

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

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

</LinearLayout>

答案 1 :(得分:1)

使用这个......这可能有用......

代码:

getMenuInflater().inflate( R.menu.main, menu );
mSpinnerItem = menu.findItem( R.id.menu_spinner );
setupSpinner( mSpinnerItem ); 

菜单XML:

 <item
    android:id="@+id/menu_spinner"
    android:actionViewClass="android.widget.Spinner"
    android:visible="false"
    android:showAsAction="always"/>

答案 2 :(得分:0)

永远不要使用NAVIGATION_MODE_LIST和onNavigationItemSelected它是不值得的!

     @Override
     public boolean onNavigationItemSelected(int position, long itemId)

    You also cannot use menu's to do this:

   @Override public boolean onCreateOptionsMenu(Menu menu)
  {
    getMenuInflater().inflate(R.menu.activity_main, menu);

due to inflation ordering.

原因:

(1) it generates a "hidden" spinner, for which you can not get the id.
(2) you cannot customize this spinner
(3) you save 30 lines of code, but are permanently limited if you want to add a second bi-directional spinner 
(4) not even in the special case of "simple code" (one spinner), you lose to much.
(5) you cannot use tabs.

the key is actionBar.setCustomView(R.layout.action_bar_custom);
and spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()...
for each spinner.

相信我,我在每个解决方案中都失去了几个小时。