Spinner 改变字体,颜色等...
16lz
2021-01-26
原生的Spinner控件是无法更改字体和颜色的...
从下面的代码可以看出...红色的标注显示使用的是Android默认的布局..代码来自于Apidemo.
1 Spinners1 = (Spinner)findViewById(R.id.spinner1);2 ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(
3 this ,R.array.colors,android.R.layout.simple_spinner_item);
4 adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
5 s1.setAdapter(adapter);
6 s1.setOnItemSelectedListener(
7 new OnItemSelectedListener(){
8 public void onItemSelected(
9 AdapterView <?> parent,Viewview, int position, long id){
10 showToast( " Spinner1:position= " + position + " id= " + id);
11 }
12
13 public void onNothingSelected(AdapterView <?> parent){
14 showToast( " Spinner1:unselected " );
15 }
16 });
通过查找源码...看到android.R.layout.simple_spinner_dropdown_item.xml
看下面的XML.
1 <? xmlversion="1.0"encoding="utf-8" ?>2 <!--
3 /*//device/apps/common/assets/res/any/layout/simple_spinner_item.xml
4 **
5 **Copyright2008,TheAndroidOpenSourceProject
6 **
7 **LicensedundertheApacheLicense,Version2.0(the"License");
8 **youmaynotusethisfileexceptincompliancewiththeLicense.
9 **YoumayobtainacopyoftheLicenseat
10 **
11 **http://www.apache.org/licenses/LICENSE-2.0
12 **
13 **Unlessrequiredbyapplicablelaworagreedtoinwriting,software
14 **distributedundertheLicenseisdistributedonan"ASIS"BASIS,
15 **WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
16 **SeetheLicenseforthespecificlanguagegoverningpermissionsand
17 **limitationsundertheLicense.
18 */
19 -->
20 < CheckedTextView xmlns:android ="http://schemas.android.com/apk/res/android"
21 android:id ="@android:id/text1"
22 style ="?android:attr/spinnerDropDownItemStyle"
23 android:singleLine ="true"
24 android:layout_width ="fill_parent"
25 android:layout_height ="?android:attr/listPreferredItemHeight"
26 android:ellipsize ="marquee" />
里面实际上是一个CheckedTextView,而CheckedTextView,又继承自TextView.所以我们可以自己定义一个只有TextView的XML文件...
里面可以随意设置TextView的属性..比如字体...颜色等等.... 然后替换adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);的xml...这样就能改变字体之类的属性了...
至此...相信大家都应该能明白了吧...
更多相关文章
- 以修改字体为例谈Android的listView开发优化
- Android:Animator使用指南
- Android(安卓)代码模拟ListView item的点击事件并且改变背景颜色
- Android基本动画详解
- Android中contentDescription属性作用
- Android(安卓)4.0 开发者指南—— Resource Types - More Types
- Android(安卓)declare-styleable:自定义控件的属性(attr.xml,Type
- 一个WebView Native Crash分析过程,居然是字体库引入的Bug
- Android(安卓)修改actionbar标题的颜色和背景色