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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值