Android 实例讲解 Spinner样式修改

对于android项目开发来说,常常会用到Spinner实现下拉框的效果。而对于Spinner加载适配器的方法有多种:

1.直接加载android自带的ArrayAdapter,SimpleAdapter;

2.自定义继承BaseAdapter的适配器。

对于适配器加载自定义的xml布局文件,修改该Spinner样式较简单,就是在定义的xml布局文件中修改显示的样式就可以。但对于加载android自带的xml布局文件,有时会出现不是项目所需要的效果。主要问题有下拉几个:

1.Spinner本身背景显示样式;

2.Spinner中文本框显示样式;

3.Spinner下拉菜单框显示样式;


下面通过实例解决上面提出的几个样式问题:

<span style="font-size:18px;">package com.example.spinnerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

	private Spinner spinner;
	private Spinner spinnerTwo;
	private String[] datas;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		spinner = (Spinner) this.findViewById(R.id.spinner);
		spinnerTwo = (Spinner) this.findViewById(R.id.spinnerTwo);
		datas = new String[] { "张三", "李四", "王五", "赵六" };

		//原生态样式,以android.R.layout.simple_spinner_dropdown_item为例,其他修改类似
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, datas);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		//根据原生态样式改变而来的自定义样式
		//Spinner中文框显示样式
		ArrayAdapter<String> adapterTwo = new ArrayAdapter<String>(this,
				R.layout.my_simple_spinner_self_item, datas);
		//Spinner下拉菜单显示样式
		adapterTwo
				.setDropDownViewResource(R.layout.my_simple_spinner_dropdown_item);
		spinnerTwo.setAdapter(adapterTwo);
	}

}
</span>

由MainActivity.java中以android.R.layout.simple_spinner_dropdown_item为例,其中android.R.layout.simple_spinner_dropdown_item系统自身的xml布局文件如下:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, 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.
*/
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>
</span>
而我们需要实现上面需要实现的样式,只需在其基础上进行修改就可以了。

1.修改Spinner本身背景色

a. 设置背景色选择器spinner_selector.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@color/blue" /> 
    <item android:state_focused="true" android:drawable="@color/blue" /> 
    <item android:drawable="@color/white" /> 
</selector> </span>

b. 颜色设置 color.xml

<span style="font-size:18px;"><resources>
	<color name="white">#FFFFFF</color>
	<color name="blue">#0000FF</color>
</resources>
</span>
c.背景色设置activity_main.xml

<span style="font-size:18px;"><LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.spinnerdemo.MainActivity$PlaceholderFragment" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" />

    <!-- Spinner自身背景色需设置:android:background="@drawable/spinner_selector" -->
    <Spinner
        android:id="@+id/spinnerTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center_vertical" />

</LinearLayout></span>
2.Spinner中文本显示样式

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, 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.
*/
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:textColor="#0000FF"
    android:gravity="center"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:ellipsize="marquee"
    android:textAlignment="inherit"
    android:background="#FFFFFF"/>
</span>

3.Spinner下拉框显示样式
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, 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.
*/
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#00FF00"
    android:ellipsize="marquee"
    android:gravity="center"
    android:singleLine="true"
    android:textAlignment="inherit"
    android:textColor="#FF0000"
    android:textSize="24sp" />
</span>

上面就是Spinner样式设置的所有内容,可以试试看。
源码地址:http://download.csdn.net/detail/a123demi/7931263

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。