自定义Spinner,需要定制的有默认背景、按下背景,显示时每一项的样式以及弹出时每一项的样式四个方面。下面举个简单例子。
</br>
1、准备两张图片,处理为.9png,分别为spinner.9.png和spinner_press.9.png,作为背景,如下图所示
![img_f0209cdf6e4e5742ab65d7bcc86ee49e.png](https://yqfile.alicdn.com/img_f0209cdf6e4e5742ab65d7bcc86ee49e.png?x-oss-process=image/resize,w_1400/format,webp)
spinner.9.png
![img_2593a759b9e2c463214828d62f9ca8cf.png](https://yqfile.alicdn.com/img_2593a759b9e2c463214828d62f9ca8cf.png?x-oss-process=image/resize,w_1400/format,webp)
spinner_press.9.png
2、在drawable中定义spinner_selector.xml,定义两种状态分别对应上面两种颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/spinner_press" /><!--按下时效果-->
<item android:state_pressed="false"
android:drawable="@drawable/spinner" /><!--默认效果-->
</selector>
3、定义一个style,也可以直接放在布局中,我为了复用拿出来单独定义了
<!-- spinner -->
<style name="spinner_style">
<item name="android:background">@drawable/spinner_selector</item>
<item name="android:paddingLeft">5dip</item>
</style>
4、在布局的Spinner中引用上述style
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/field_item_spinner_content"
style="@style/spinner_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp" />
</RelativeLayout>
5、每一项的样式 simple_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:singleLine="true"
android:textColor="#808080" />
6、弹出时的每一项显示布局 my_drop_down_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textColor="#000000"
android:textSize="15dp" >
</TextView>
7、定义一个字符数组,作为数据显示
<string-array name="meinv">
<item>美女1</item>
<item>美女2</item>
<item>美女3</item>
<item>美女4</item>
<item>美女5</item>
<item>美女6</item>
<item>美女7</item>
</string-array>
8、Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.field_item_spinner_content);
//资源转[]
String meinv[] = getResources().getStringArray(R.array.meinv);
//构造ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.simple_spinner_item, meinv);
//设置下拉样式以后显示的样式
adapter.setDropDownViewResource(R.layout.my_drop_down_item);
spinner.setAdapter(adapter);
}
}
9、测试效果
![img_da5abe155d1e6f4413b309f4becf47f0.gif](https://yqfile.alicdn.com/img_da5abe155d1e6f4413b309f4becf47f0.gif?x-oss-process=image/resize,w_1400/format,webp)
自定义Spinner.gif