2

我有以下

微调器

我正在使用操作栏和微调器,但我想要添加一个像 City subtitles => All , City 1 , City 2 的字幕

喜欢这张图片 微调器

我有以下菜单 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;
    }
4

1 回答 1

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);
   }

以及 textView 的 getClick 事件和 Open AlertDialogwithsetSingleChoiceItems

于 2016-12-05T05:06:54.437 回答