如何将我的menu.xml传递给NAVIGATION_MODE_LIST中的Sherlock actionBar

时间:2014-03-06 11:21:25

标签: android

我只是用库Sherlock创建actionBar。一切正常,但现在我将插入一个下拉列表菜单到栏顶部。这些示例使用字符串数组构建下拉菜单:

/** An array of strings to populate dropdown list */
String[] actions = new String[] {
    "Bookmark",
    "Subscribe",
    "Share"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),    R.layout.sherlock_spinner_item, actions);

我会:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),    R.layout.sherlock_spinner_item, R.menu.list_news);

R.menu.list_news:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >


<item 
    android:id="@+id/classifica"
    android:title="@string/classifica"
    android:showAsAction="withText"        
/>

<item 
    android:id="@+id/next_match"
    android:title="@string/next_match"
    android:showAsAction="withText"        
/>

<item 
    android:id="@+id/risultati"
    android:title="@string/risultati"
    android:showAsAction="withText"        
/>

<item 
    android:id="@+id/top_ten"
    android:title="@string/top_ten"
    android:showAsAction="withText"        
/>

</menu>

这是我的活动。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Create an array adapter to populate dropdownlist */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),    R.layout.sherlock_spinner_item, MY_MENU_XML);


    /** Enabling dropdown list navigation for the action bar */
    getSupportActionBar().setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);


    /** Defining Navigation listener */
    ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            //Toast.makeText(getBaseContext(), "Current Action : " + actions[itemPosition]  , Toast.LENGTH_SHORT).show();
            return false;
        }
    };

    /** Setting dropdown items and item navigation listener for the actionbar */
    getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);        
    adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    LoadData task = new LoadData();
    task.execute();
}

此处的示例:http://wptrafficanalyzer.in/blog/adding-drop-down-navigation-to-action-bar-in-pre-honeycomb-versions-using-sherlock-library/

1 个答案:

答案 0 :(得分:0)

您需要在下拉列表中显示文本列表,然后使用下面的代码。

mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        mActionBar.setDisplayHomeAsUpEnabled(true);

        mArrayDropDown=getResources().getStringArray(R.array.dropdown);

        ArrayAdapter<String> mAdapter=new ArrayAdapter<String>(ListNavActivity.this, android.R.layout.simple_spinner_item, mArrayDropDown);

        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        mActionBar.setListNavigationCallbacks(mAdapter, this);

这里的mArrayDropDown是String数组。 虽然这段代码适用于Compact Libarary v7,但它与Sherlock非常相似。