spinner改变字体颜色,大小等
2015-12-02 10:40
239 查看
原生的Spring 控件是无法更改字体和颜色的...
从下面的代码可以看出...红色的标注显示使用的是Android默认的布局..代码来自于Apidemo.
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 }
16 });
通过查找源码...看到android.R.layout.simple_spinner_dropdown_item.xml
看下面的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...这样就能改变字体之类的属性了...
至此...相信大家都应该能明白了吧...
自定义的TextView可如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:gravity="left"
android:paddingLeft="8dip"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
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...这样就能改变字体之类的属性了...
至此...相信大家都应该能明白了吧...
从下面的代码可以看出...红色的标注显示使用的是Android默认的布局..代码来自于Apidemo.
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 }
16 });
通过查找源码...看到android.R.layout.simple_spinner_dropdown_item.xml
看下面的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...这样就能改变字体之类的属性了...
至此...相信大家都应该能明白了吧...
自定义的TextView可如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:gravity="left"
android:paddingLeft="8dip"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
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...这样就能改变字体之类的属性了...
至此...相信大家都应该能明白了吧...
相关文章推荐
- EditPlus 3设置字体大小(附图)
- jQuery实现自动调整字体大小的方法
- 动态调整textarea中字体的大小代码
- JavaScript 文本域字体大小选择功能
- Android下拉列表(Spinner)效果(使用C#和Java分别实现)
- Android中Spinner控件之键值对用法实例分析
- android应用开发之spinner控件的简单使用
- Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
- Android Dialog 设置字体大小的具体方法
- Android控件之Spinner用法实例分析
- js实现动态改变字体大小代码
- Spinner的总结
- android.widget.Spinner
- cocos2d 字体颜色
- Spinner选中文字颜色和下拉颜色不同的设置
- android 下拉列表 Spinner
- 自定义在两种屏幕尺寸中使用spinner
- 修改Eclipse中中文的字体大小
- 基于js和css3的缓冲图标
- linux下 C编程改变输出字体颜色