带有默认图标和箭头的Spinner

3
假设这是一个“Spinner”,你如何设置默认图标?
目前我的只显示第一项(蓝牙)的图标。 enter image description here

我更新了我的回答,如果对你有帮助的话。 - Jon
3个回答

4
我相信这个具体的例子是 ShareActionProvider,它是 ActionProvider 的一个特殊子类。
如果你在菜单的xml中添加了一个 ActionProvider,你可以将其视为菜单项并添加图标。
来自 ActionProvider 文档:
<item android:id="@+id/my_menu_item"
 android:title="Title"
 android:icon="@drawable/my_menu_item_icon"
 android:showAsAction="ifRoom"
 android:actionProviderClass="foo.bar.SomeActionProvider" />

由于我没有使用过这些,所以不知道是否所有的ActionProvider都有旋转三角形... 我猜是吗?

更新:

我刚想到SpinnerAdapter的工作原理还有另一种方法可以实现。如果您在Spinner中使用自定义的SpinnerAdapter子类,它将有两个方法,您可以按照以下方式操作:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
    // return the view to display when you are looking at the dropdown,
    // so this will probably be a TextView and/or an ImageView
    }

@Override
public View getView(int position, View convertView, ViewGroup parent)
    {
    // This will show when the item at the provided position is 
    // selected. At this point you could return the ImageView you want to 
    // always appear at the top, such as the share icon.
    }

+1 为你的知识点赞,但是在我的情况下,我无法给你支票,因为它是一个 spinner。 - Matt
没问题,很高兴你找到了解决方案! - Jon

0
今天我浪费了时间,所以我想提供正确的答案,即使这个帖子看起来已经死了。
首先,我怀疑像@Jon一样,仅仅调用神秘的ActionProvider就会为我们添加旋转三角形。但事实并非如此。
其次,谷歌正在作弊,并违反自己的ActionBar图标指南来实现这一点。如果你看一下http://androiddrawables.com/Menu.html,你会发现分享图标特别在其角落里加入了类似于旋转三角形的东西。
基本上,这意味着你的主要假设——它是一个Spinner——是错误的。它只是一个被黑客攻击成看起来像Spinner的按钮。
因此,如果你想要创建自己的自定义图标,就按照指定ActionBar按钮图标的常规说明进行操作(http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems)。如果你有像我一样的文本按钮,那么这种方法是行不通的。
我最终所做的是利用支持库中提供的一些旋转器背景九宫格文件,以及添加到应用于相关按钮的样式中的属性'actionBarItemBackground'。
<item name="android:actionBarItemBackground" tools:ignore="NewApi">@drawable/abc_spinner_ab_default_holo_light</item>
<item name="actionBarItemBackground">@drawable/abc_spinner_ab_default_holo_light</item>

在尝试此 hack 时,请记住 ActionBar with AppCompat actionBarItemBackground not working


-2

我用一个"hack"解决了这个问题,就是将一个ImageView放在Spinner的上面。

<Spinner
    android:layout_width="40dp"
    android:layout_height="40dp"
    />

<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_share"
    android:adjustViewBounds="true"
    android:background="@color/same_as_parent_background"
    />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接