Android操作栏下拉菜单

时间:2013-06-10 08:17:44

标签: android drop-down-menu menu android-actionbar

我想在我的下拉菜单中添加图标,但我不知道如何添加它。这是我的代码。我在我的操作栏中使用微调器作为下拉菜单。非常感谢你,对不起我的英语。

活动Main.java

public class MainActivity extends Activity implements OnClickListener {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBar();
        View btnAbout = findViewById(R.id.about_button);
        btnAbout.setOnClickListener(this);

    }

    public void actionBar(){
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);    
        actionBar.setDisplayShowTitleEnabled(false);

        SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list,
                  android.R.layout.simple_spinner_dropdown_item);


        ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                lanuchItemSelected(itemPosition);
                return true;

        }
        };
        actionBar.setListNavigationCallbacks(mSpinnerAdapter, navigationListener ); 
    }

    private void lanuchItemSelected(int item){
        Intent i;
        switch(item){
        case 3:
            i = new Intent(this,SettingsActivity.class);
            startActivity(i);
            break;
        case 4:
            i = new Intent(this,About.class);
            startActivity(i);
            break;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        //getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
        case R.id.settings:
            Intent i = new Intent(this,SettingsActivity.class);
            startActivity(i);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View v){
        switch(v.getId()){
        case R.id.about_button:
            Intent i = new Intent(this,About.class);
            startActivity(i);
            break;
        }
    }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sudoku"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        >

        <activity
            android:name="com.example.sudoku.MainActivity"
            android:label="@string/app_name" 
            android:theme="@style/ThemeWithActionBarOverlay"
           >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".About"
            android:label="@string/about_title"
            android:theme="@android:style/Theme.Dialog" >



            </activity>
            <activity
                android:name=".SettingsActivity"
                android:label="@string/settings_title"
                android:theme="@style/ThemeWithActionBarOverlay"
                android:parentActivityName=".MainActivity"
             >
            <meta-data
                android:name="android.support.PARENT_ACTIVTY"
                android:value=".MainActivity"/>
                >

            </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您必须继承SpinnerAdapter的子类,并且在其getDropDownView方法中,您必须提供带有图标和标签的自定义布局。

你可以在这里找到一个例子: http://android-er.blogspot.co.uk/2010/12/custom-arrayadapter-for-spinner-with.html