【问题标题】:Change Spinner dropdown icon in toolbar更改工具栏中的微调器下拉图标
【发布时间】:2020-05-19 12:39:42
【问题描述】:

我正在尝试将工具栏中的图标用作微调器...这是我的menu.xml

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_search_cat"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        android:title=".."/>
    <item
        android:id="@+id/action_filter_rating"
        app:showAsAction="always"
        android:actionViewClass="android.widget.Spinner"
        android:icon="@drawable/ic_star" // THIS LINE HAVE NO EFFECT 
        android:title=".." />
    <item
        android:id="@+id/action_manage_category"
        android:title=".."
        app:showAsAction="never" />
    <item
        android:id="@+id/action_export_csv"
        android:title="@string/export"
        app:showAsAction="never" />

现在下拉图标是默认的(android.widget.Spinner),有没有办法覆盖那个图标?我想将微调器保留在菜单项中。 我想改变这个图标:

我将接受所有其他解决方案,以在工具栏中实现自定义图标,在相同位置,作为微调器(带下拉菜单)工作。谢谢!

【问题讨论】:

  • 你能发布整个 menu.xml 文件吗?
  • 是的,现在...
  • 您是否还可以发布与该微调器的适配器相关的代码...在单击该代码的图标后如何填充微调器...
  • 是的,我还没有编写那个代码。我正在尝试使用正确的图标设置微调器之前

标签: android xml android-layout android-spinner android-toolbar


【解决方案1】:

在 MenuItem 单击时,您可以膨胀 PopupMenu,其行为类似于 Spinner。

menu.xml:

    <item
    android:id="@+id/action_filter_rating"
    app:showAsAction="ifRoom"
    android:icon="@drawable/ic_collections_white_24dp"
    android:enabled="true"
    android:title="@string/action_filter_rating"/>

活动菜单监听器:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_filter_rating) {
        View anchor = findViewById(R.id.action_filter_rating);
        PopupMenu popup = new PopupMenu(this, anchor);
        popup.getMenuInflater().inflate(R.menu.your_spinner_menu, popup.getMenu());
        popup.getMenu().add("You can dynamically add items");
        popup.setOnMenuItemClickListener(...);
        popup.show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多