如何从图标操作栏实现下拉列表

时间:2013-03-03 17:46:58

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

我可以在操作栏上获得下拉菜单,但是当我点击图标时,我没有得到如何做同样的事情。

enter image description here

这里我是如何默认获得的下拉菜单,但我希望通过单击图标来获得相同的内容。即如果我点击左侧的android图标我希望dropdwon出现。

有什么办法吗?..

这是我的代码。

package com.example.actionbarsample;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

      String []data1={"Brainbitz:java","Brainbitz:Android","Brainbitz:Embedded Systems","Brainbitz:PHP"};
      String []data2={"6month Course","3month Course","3month Course","3month Course"};
      Integer[] images={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};

      /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        MyAdapter mSpinnerAdapter = new MyAdapter(this, R.layout.spinner, data1);

       actionBar.setListNavigationCallbacks(mSpinnerAdapter, nav);

      //  Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);
       // mySpinner.setAdapter(new MyAdapter(this, R.layout.spinner, data1));
    }

    OnNavigationListener nav = new OnNavigationListener() {

    @Override
    public boolean onNavigationItemSelected(int arg0, long arg1) {
        // TODO Auto-generated method stub
        return false;
    }
};


    public class MyAdapter extends ArrayAdapter<String>
    {

            public MyAdapter(Context context, int textViewResourceId,
                        String[] objects) {
                  super(context, textViewResourceId, objects);
                  // TODO Auto-generated constructor stub
            }
            @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.spinner, parent, false);
            TextView label=(TextView)row.findViewById(R.id.textView1);
            label.setText(data1[position]);

            TextView sub=(TextView)row.findViewById(R.id.textView2);
            sub.setText(data2[position]);

            ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
            icon.setImageResource(images[position]);

            return row;
            }

   }


    }

0 个答案:

没有答案