Android(java)学习笔记96:如何改变spinner系统自带的字体和颜色

1.首先我们要知道spinner系统自带字体和颜色本质:

原生的Spring 控件是无法更改字体和颜色的...

从下面的代码可以看出...红色的标注显示使用的是Android默认的布局..

 1 Spinner s1 = (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, View view, int position, long id) {

10                         showToast("Spinner1: position=" + position + " id=" + id);

11                     }

12 

13                     public void onNothingSelected(AdapterView<?> parent) {

14                         showToast("Spinner1: unselected");

15                     }

上面中的:

android.R.layout.simple_spinner_item //android.R.layout.simple_spinner_item是由Android提供的一种标准spinner的布局
android.R.layout.simple_spinner_dropdown_item //声明当控件打开时的外观:为系统提供的simple_spinner_dropdown_item   
都是系统自带的


通过查找源码...看到android.R.layout.simple_spinner_dropdown_item.xml,如下:
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <!--

 3 /* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml

 4 **

 5 ** Copyright 2008, The Android Open Source Project

 6 **

 7 ** Licensed under the Apache License, Version 2.0 (the "License"); 

 8 ** you may not use this file except in compliance with the License. 

 9 ** You may obtain a copy of the License at 

10 **

11 **     http://www.apache.org/licenses/LICENSE-2.0 

12 **

13 ** Unless required by applicable law or agreed to in writing, software 

14 ** distributed under the License is distributed on an "AS IS" BASIS, 

15 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

16 ** See the License for the specific language governing permissions and 

17 ** limitations under the License.

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...这样就能改变字体之类的属性了...

至此...相信大家都应该能明白了吧...



2.有了上面的逻辑思路,下面的东西就是要具体实现了:

(1)我们在android工程文件中找到layout文件夹,在这个文件夹另外建立一个layout.xml文件如下:
这里我命名为custom_spiner_text_item.xml
Android(java)学习笔记96:如何改变spinner系统自带的字体和颜色

这里
custom_spiner_text_item.xml文件内容如下:
 1 <?xml version="1.0" encoding="utf-8"?>

 2  <TextView xmlns:android="http://schemas.android.com/apk/res/android"

 3      android:id="@+id/textViewCustom"

 4      android:layout_width="match_parent"

 5      android:layout_height="wrap_content"

 6      android:ellipsize="marquee"

 7      android:singleLine="true"

 8      android:textAlignment="inherit"

 9      android:textColor="#222288"

10      android:textSize="20sp" />

这里我定义我想要的需要的属性,当然你也可以定义你想要的属性,勾画出你想需要的TextView

 

(2)这时候我们已经定义好了custom_spiner_text_item.xml,接下来当然是怎么用它,如下:

相信用过spinner这个控件的朋友不会陌生下面的代码语句:这里我是截取部分代码说明custom_spiner_text_item.xml的用法。

 1  List<ScanResult> localList = MainActivity.this.wifi.getScanResults();//用来获得手机扫描到的所有wifi的信息

 2           if (localList.size() < 1)//判断List中的元素的个数。localList.size() <1:表示List没有元素,元素的个数表示:搜索到无线网络个数

 3          {

 4            MainActivity.this.connectBtn.setEnabled(false);

 5             Toast.makeText(MainActivity.this.getApplicationContext(), "没有找到无线网络", 1).show();

 6           return;

 7           }

 8          Spinner localSpinner = (Spinner)MainActivity.this.findViewById(R.id.spinner1);

 9          ArrayList<String> localArrayList = new ArrayList<String>();

10          Iterator<ScanResult> localIterator = localList.iterator();       

11        while (true)

12          {

13           if (!(localIterator.hasNext()))

14            {    

15                //配置spinner控件展现样式,spinner只是承载多项数据,下面是以何种方式展现这些数据

16                ArrayAdapter<String> localArrayAdapter = new ArrayAdapter<String>(MainActivity.this.getApplicationContext(),

android.R.layout.simple_spinner_item , localArrayList);
17 localArrayAdapter.setDropDownViewResource(R.layout.custom_spiner_text_item); 18 19 localSpinner.setAdapter(localArrayAdapter); 20 localSpinner.setPrompt("请选择搜索到网络:"); 21 MainActivity.this.connectBtn.setEnabled(true); 22 return; 23 }
}

 

这样就可以了,R.layout.custom_spiner_text_item表示调用layout文件夹下的custom_spiner_text_item.xml资源

这样就达到我们的需求了,嘿嘿,是不是很简单!

你可能感兴趣的:(android)