ANDROID笔记: ActionBar(三)之ActionBar List

package com.example.android_actionenu.test;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.example.android_actionbar.R;

public class ListActionBarActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listactionbar);
        // 生成一个SpinnerAdapter
        SpinnerAdapter adapter = ArrayAdapter.createFromResource(this,
                R.array.actionarrays,
                android.R.layout.simple_spinner_dropdown_item);
        final String[] arrys = getResources().getStringArray(
                R.array.actionarrays);

        ActionBar actionBar = getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setListNavigationCallbacks(adapter,
                new OnNavigationListener() {

                    @SuppressLint("ShowToast")
                    @Override
                    public boolean onNavigationItemSelected(int itemPosition,
                            long itemId) {
                        Toast.makeText(ListActionBarActivity.this,
                                arrys[itemPosition], 200).show();
                        return true;
                    }
                });
    }

}

/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Android_ActionBar</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="actionarrays">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
        <item>item4</item>
    </string-array>

</resources>

 

posted @ 2013-11-06 19:48  skyko  阅读(275)  评论(0编辑  收藏  举报