温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - Place a Spinner in Action bar

java - 将 Spinner 放置在操作栏中

发布于 2020-03-28 23:30:21

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

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

查看更多

查看更多

提问者
random1234
被浏览
18
Avinash 2020-02-03 18:04

试试这个

从create方法的活动中调用此方法,并同时实现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" />

发布
问题

分享
好友

手机
浏览

扫码手机浏览