在Android中, Spinner组件就是下拉列表. 弹出一个选项列表,允许你从列表中选择一项。
布局的XML文件如下
<Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> |
Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class SpinnerExample extends Activity { private String array_spinner[]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); array_spinner=new String[5]; array_spinner[0]="1"; array_spinner[1]="2"; array_spinner[2]="3"; array_spinner[3]="4"; array_spinner[4]="5"; Spinner s = (Spinner) findViewById(R.id.Spinner01); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_spinner); s.setAdapter(adapter); } } |
根据value设置Spinner,当然用position设置Spinner会更好,但是有些场景下只能用value来设置。
spinner.setSelection(adapter.getPosition(account.type)); |
设置弹出下拉列表的样式见设置Spinner弹出框/下拉框的样式

Thanks for sharing this useful content.
It’s very helpful for me!
Thanks for the informative article, it was a good read and I hope its ok that I share this with some facebook friends. Thanks.
I do not know if it’s just me or if everybody else encountering issues with your site. It seems like some of the text in your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them as well? This might be a problem with my browser because I’ve had this happen previously. Cheers
my site is [url=http://cheapfamilyvacations.blogspot.com/]family vacation ideas[/url].
Thanks for that awesome posting. Useful, and it saved MUCH time!
Beach wedding
Pingback: [android]设置Spinner弹出框/下拉框的样式 | keeper – 分享知识、分享快乐