2013-08-28 57 views
3

假设这是一个Spinner,您如何设置默认图标?带默认图标的微调框+箭头

目前,矿井只是显示的第一个项目的图标(蓝牙)

enter image description here

+0

我更新了我的答案,以防万一对你有帮助 – Jon

回答

4

我认为,具体的例子是ShareActionProvider,这是ActionProvider

如果你是一个特殊的sublcass在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■找微调角落三角...我的猜测是肯定的?

UPADTE:

我只是想着如何SpinnerAdapter作品和有另一种方式来做到这一点。如果你在你的Spinner使用SpinnerAdapter自定义子类,将有2种方法,你可以做到以下几点:

@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. 
    } 
+0

+1为知识,但我不能给你检查,因为在我的情况下,它是一个微调 – Matt

+0

无后顾之忧,很高兴你找到了解决方案! – Jon

-2

我“解决”这个问题,黑客攻击,这是把一个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" 
    /> 
0

今天浪费了时间,所以我想我会提供正确的答案,即使此线程显示死亡。

首先,我曾怀疑为@乔恩这样做只是调用不可思议的神秘ActionProvider将添加微调三角代表我们。它不是。

其次,事实证明谷歌是作弊并违反了他们自己的ActionBar图标指引来实现此目的。如果您查看http://androiddrawables.com/Menu.html,您会看到共享图标专门有一个类似微调的三角形入侵到了它的角落。

基本上,这意味着您的主要假设 - 它是一个Spinner - 是错误的。这只是一个按钮,被黑了看起来像一个微调。

所以,你可以在静脉创建自己的自定义图标,如果你想要的,只要按照平时的指令指定动作条按钮图标(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> 

尝试这种黑客攻击时,你会想记住ActionBar with AppCompat actionBarItemBackground not working