0%

自定义Spinner显示的样式(修改颜色等)

这里指的Spinner显示的样式,并不是指在点击Spinner后弹出的选择框的样式,而是直接显示在屏幕上的那个按钮的样式。我有个项目需要使用Spinner,但使用默认的样式又与现有UI不匹配,需要稍稍改一下。 这个样式其实是由适配器指定的,如:

ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,list);

这里指定使用android.R.layout.simple_spinner_item,我们打开这个文件(当然,你要先下载Android源码),发现里面其实就是一个TextView,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" />

现在知道怎么做了吧?自己写个xml,加上一个TextView,id也是@android:id/text1,其他的就看你自己需要了。