如何在操作栏中为微调器添加字幕

时间:2013-08-24 14:15:34

标签: android android-actionbar spinner

我有以下

Spinner

我正在使用操作栏和微调器,但是我想要添加一个字幕就像城市字幕=>全部,城市1,城市2

像这张图片一样  Spinner

我有以下菜单xml文件

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

<item
        android:id="@+id/filter1"
        android:showAsAction="ifRoom"
        android:orderInCategory="1"
        android:actionViewClass="android.widget.Spinner"
        />

<item
        android:id="@+id/filter2"
        android:showAsAction="ifRoom"
        android:orderInCategory="1"
        android:actionViewClass="android.widget.Spinner" />

</menu>

,这在活动

 public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_menu, menu);
        mSpinnerItem1 = menu.findItem(R.id.filter1);
        mSpinnerItem2 = menu.findItem(R.id.filter2);

        setupSearchView();
        View view1 = mSpinnerItem1.getActionView();
        if (view1 instanceof Spinner) {
            Spinner spinner = (Spinner) view1;
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.filters, android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                    mDbHelper.open();
                    showResults();
                }

                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

        }

        View view2 = mSpinnerItem2.getActionView();
        if (view2 instanceof Spinner) {
            Spinner spinner = (Spinner) view2;
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.cities, android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {

                    mDbHelper.open();
                    showResults();

                }

                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

我们可以以不同的方式实现此功能。

<强> custom_toolbar.xml

<android.support.v7.widget.Toolbar>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
             .... For Title
        />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
              ....//For Select things...
            />

            <ImageView
                .... down arrow image
            />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.Toolbar>

添加 mainlayout.xml

 <include
   android:id="@+id/toolbar"
   layout="@layout/custom_toolbar" />

活动

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
   if (toolbar != null) {
       setSupportActionBar(toolbar);
   }

使用AlertDialog

获取textView的getClick事件并打开setSingleChoiceItems