Android的Spinner

时间:2023-03-09 20:49:01
Android的Spinner

使用Spinner遇到不少坑啊

3.自定义spinner样式

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="android:spinnerItemStyle">@style/AppTheme.SpinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">@style/AppTheme.SpinnerDropDownItemStyle</item>
</style>
<style name="Spinner" parent="Widget.AppCompat.Spinner">
<item name="android:popupBackground">@drawable/abc_popup_background_mtrl_mult</item>
</style> <style name="AppTheme.SpinnerItemStyle" parent="Widget.AppCompat.TextView.SpinnerItem">
<item name="android:textColor">#ffffff</item>
</style> <style name="AppTheme.SpinnerDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner">
<item name="android:background">?attr/selectableItemBackground</item>
<item name="android:textColor">#212121</item>
</style>

1.调用了setSelection()之后不会马上调用onItemSelected

2.想调用setSelection之后不调用onItemSelected,需要写如下代码,注意顺序

spinner.setSelection(spinnerItem, true);//这里必须传true参数,否则还是会调用onItemSelected

spinner.setOnItemSelectedListener(this);