在操作栏中放置一个微调器

Place a Spinner in Action bar

我的问题很简单,您将如何在操作栏中设置一个 Spinner/dropdown 列表(代替标题)?

我知道这里还有其他类似的问题,但我能找到的所有答案都非常陈旧并且使用了已弃用的代码或链接到 Android 文档中不再存在的某些指南。

试试这个

在创建方法上从 activity 调用它并实现 AdapterView.OnItemSelectedListener。

   String[] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "thursday","Sunday"}

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.custom_app_bar_spinner);

    Spinner spinner = (Spinner) findViewById(R.id.spinnerMethodInterviews);
    Spinner spinnerSecond = (Spinner) findViewById(R.id.spinnerMethodSecond);
    ArrayAdapter<String>   spinnerAdapter = new ArrayAdapter(this,
            R.layout.custom_title, android.R.id.text1, weeks);

    spinnerAdapter.setDropDownViewResource(R.layout.cutom_spinner);
    spinner.setAdapter(spinnerAdapter);
    spinnerSecond.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(this);
    spinnerSecond.setOnItemSelectedListener(this);

main_Activity.xml

   <?xml version="1.0" encoding="utf-8"?>
  <android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout
    android:id="@+id/view_top"
    android:layout_width="0dp"
    android:layout_height="50dp"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/colorAccent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">

    <Spinner
        android:id="@+id/spinnerMethodInterviews"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        android:spinnerMode="dropdown"/>

    <Spinner
        android:id="@+id/spinnerMethodSecond"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:spinnerMode="dropdown"/>

</android.support.constraint.ConstraintLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/view_top"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
</android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

custom_spinner.xml

   <CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@android:color/white" />

custom_title.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@android:color/white" />