简体   繁体   English

旧版Android版中的ICS Spinner(“下拉”而不是对话框)

[英]ICS Spinner (“drop-down” rather than dialog) in older Android versions

I'm trying to mimic the newer Ice Cream Sandwhich version of the spinner, where it looks more like a drop-down list rather than a pop-up dialog. 我试图模仿较新的Ice Cream Sandwhich版本的微调器,它看起来更像是一个下拉列表而不是弹出对话框。 See this link to get an idea of what I'm talking about. 请参阅此链接以了解我在说什么。 I read some posts stating to use ActionBarSherlock to get the desired effect. 我读了一些帖子,说明要使用ActionBarSherlock来获得所需的效果。 However, this is created for use in the action bar only, so, how do I take the ICS Spinner out of the action bar? 但是,这仅用于操作栏,因此,如何将ICS Spinner从操作栏中取出?

There is a pretty good answer here, yet, I feel like this is a little bit overkill? 这里有一个很好的答案,但是,我觉得这有点矫枉过正? Is there an easier way? 有更容易的方法吗?

First off, I referenced this link to whether or not I should answer my own question. 首先,我引用了这个链接 ,我是否应该回答我自己的问题。 I felt this can be very useful to someone faced with a similar problem, so I apologize if this is not proper etiquette for this website (to answer your own question). 我觉得这对面临类似问题的人非常有用,所以如果这不是本网站的正确礼仪,我会道歉(回答你自己的问题)。

Now, I have stumbled around trying to find a solution for this problem and with trial and error I have succeeded. 现在,我偶然发现试图找到解决这个问题的方法,经过反复试验,我已经成功了。 So, once you have ActionBarSherlock SDK downloaded and set up in your project, create your layout that will incorporate the spinner: 因此,一旦您在项目中下载并设置了ActionBarSherlock SDK,请创建将包含微调器的布局:

    <com.actionbarsherlock.internal.widget.IcsSpinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_margin="10sp"
            android:layout_centerHorizontal="true"
            android:textSize="18sp" />

The above code will use the ICS version of the spinner which is in the ActionBarSherlock library. 上面的代码将使用ActionBarSherlock库中的微调器的ICS版本。 Next, in your Activity declare and instantiate (using casting) the spinner object. 接下来,在您的Activity声明并实例化(使用强制转换)微调器对象。 But note that you do not use the normal Spinner class, you use the IcsSpinner class found in the ActionBarSherlock library: 但请注意,您不使用普通的Spinner类,而是使用ActionBarSherlock库中的IcsSpinner类:

IcsSpinner spinner = (IcsSpinner)findViewById(R.id.spinner);

Now, you create an adapter just as you would for the normal Spinner, like so: 现在,您可以像创建普通Spinner一样创建适配器,如下所示:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, elements);
spinner.setAdapter(adapter);

Finally, you need to set up the onItemSelectedListener . 最后,您需要设置onItemSelectedListener The only major difference here is that you use IcsAdapterView.OnItemSelectedListener rather than just OnItemSelectedListener : 这里唯一的主要区别是你使用IcsAdapterView.OnItemSelectedListener而不仅仅是OnItemSelectedListener

spinner.setOnItemSelectedListener(new IcsAdapterView.OnItemSelectedListener(){
    @Override
    public void onItemSelected(IcsAdapterView<?> parent, View view, int position, long id){
    }
    @Override
    public void onNothingSelected(IcsAdapterView<?> parent){
    }
});

And that's it. 就是这样。 It's really not much different then just using the spinner object. 它与使用微调器对象真的没什么不同。 As easy as it is, it took me awhile to figure out, so, I hope this is useful to someone. 虽然很简单,但我花了一些时间才弄明白,所以,我希望这对某人有用。

Oh yeah, and don't forget to use an ActionBarSherlock theme, like so (in the manifest): 哦是的,不要忘记使用ActionBarSherlock主题,就像这样(在清单中):

android:theme="@style/Theme.Sherlock"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM