首先,我参考了this link 是否应该回答我自己的问题。我觉得这对面临类似问题的人非常有用,所以如果这不适合本网站的礼仪,我深表歉意(回答您自己的问题)。
现在,我偶然发现试图找到解决此问题的方法,经过反复试验,我成功了。因此,一旦您在项目中下载并设置了 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" />
以上代码将使用 ActionBarSherlock 库中的 ICS 版本的微调器。接下来,在您的 Activity 中声明并实例化(使用强制转换)微调器对象。但请注意,您使用的不是普通的 Spinner 类,而是使用 ActionBarSherlock 库中的 IcsSpinner 类:
IcsSpinner spinner = (IcsSpinner)findViewById(R.id.spinner);
现在,您可以像创建普通 Spinner 一样创建适配器,如下所示:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, elements);
spinner.setAdapter(adapter);
最后,您需要设置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){
}
});
就是这样。与仅使用微调器对象并没有太大区别。
虽然很简单,但我花了一段时间才弄清楚,所以,我希望这对某人有用。
哦,是的,别忘了使用 ActionBarSherlock 主题,就像这样(在清单中):
android:theme="@style/Theme.Sherlock"