ICS微调("下拉"而不是对话框)中老年的Andr​​oid版本中老年、对话框、而不是、版本

2023-09-05 07:04:10 作者:Absurd

我试图模仿新的冰淇淋Sandwhich版的微调,它看起来更像是一个下拉列表,而不是弹出式对话框。 请参阅此链接得到什么我谈论的想法。我看了一些帖子,说明使用ActionBarSherlock来获得预期的效果。然而,这在操作栏使用而创建唯一的,所以,我怎么走了ICS微调出来的动作吧?

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. 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. However, this is created for use in the action bar only, so, how do I take the ICS Spinner out of the action bar?

There是pretty的很好的答案在这里,然而,我觉得这是一个有点矫枉过正?有没有更简单的方法?

There is a pretty good answer here, yet, I feel like this is a little bit overkill? Is there an easier way?

推荐答案

首先,我引用this 链接到我是否应该回答我的问题。我觉得这可能是一个人面临着类似的问题非常有用的,所以我道歉,如果这是不恰当的礼仪本网站(回答你自己的问题)。

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).

现在,我已经迷迷糊糊周围试图找到这个问题,并与试验和错误我已经成功的解决方案。所以,一旦你ActionBarSherlock SDK下载和设置在您的项目,创建自己的布局,将纳入微调:

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:

    <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" />

以上code将利用这一点在ActionBarSherlock库微调的ICS版本。接下来,在你的活动声明和实例(使用铸造)微调对象。但请注意,不要使用正常的微调类,您使用的ActionBarSherlock库中找到的IcsSpinner类:

The above code will use the ICS version of the spinner which is in the ActionBarSherlock library. Next, in your Activity declare and instantiate (using casting) the spinner object. But note that you do not use the normal Spinner class, you use the IcsSpinner class found in the ActionBarSherlock library:

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

现在,您将创建一个适配器就像你的正常微调,像这样:

Now, you create an adapter just as you would for the normal Spinner, like so:

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

最后,您需要设置了 onItemSelectedListener 。这里唯一的主要区别是,你使用 IcsAdapterView.OnItemSelectedListener 而不仅仅是 OnItemSelectedListener

Finally, you need to set up the onItemSelectedListener. The only major difference here is that you use IcsAdapterView.OnItemSelectedListener rather than just 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.

哦对了,不要忘记使用ActionBarSherlock的主题,像这样(在清单):

Oh yeah, and don't forget to use an ActionBarSherlock theme, like so (in the manifest):

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