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

时间: 2024-08-01 22:29:22

Android 实例讲解 Spinner样式修改的相关文章

Android 实例讲解HorizontalScrollView实现左右滑动

本博文主要讲解怎么使用HorizontalScrollView实现左右滑动的效果. HorizontalScrollView实际上是一个FrameLayout ,一般通过只放置一个LinearLayout子控件.如果要使其添加其他的控件,就使用LinearLayout子控件来添加其他的控件,最后达到丰富其内容的效果.其中,LinearLayout设置的orientation布局为Horizontal.HorizontalScrollView不可以和ListView同时用,因为ListView有自

Android 实例讲解添加本地图片和调用系统拍照图片

在项目的开发过程我们离不开图片,而有时候需要调用本地的图片,有时候需要调用拍照图片.同时实现拍照的方法有两种,一种是调用系统拍照功能,另一种是自定义拍照功能.而本博文目前只讲解第一种方法,第二种方法后期在加以讲解. 添加本地图片和调用系统拍照图片主要是通过调用acitivity跳转startActivityForResult(Intent intent, int requestCode)方法和activity返回结果onActivityResult(int requestCode, int re

Android 实例讲解自定义Camera拍照和预览以及前后置摄像头切换

上一篇博文讲解了怎么去调用本地图片和调用系统拍照图片(http://blog.csdn.net/a123demi/article/details/40003695)的功能. 而本博文将通过实例实现自定义Camera的功效.具体功能如下: 1.实现自定义Camera拍照: 2.实现前后置摄像头的切换: 3.实现Camera拍照后图片缩小显示以及正常预览: 4.实现Camera拍照后图片保存: 在具体实现代码之前,我们先来了解一下Android api对实现自定义Camera的介绍. 根据api的介

Android开发之自定义Spinner样式的效果实现(源代码实现)

android系统自带的Spinner样式是远远满足不了我们实际开发过程中对Spinner UI风格的要求,因此我们肯定需要为了切合整个应用的风格,修改我们的Spinner样式.系统给我们提供了两种常见的修改方式,一个是用XML方式静态,另一个就是Java代码动态来修改啦,我们这篇文章呢主要就是介绍如何动态修改Spinner的样式.我的实现方法呢,是自己构造一个SpinnerAdapter,继承来自ArrayAdapter,重写getDropDownView(),getView()这两个方法就好

Android 依赖注入: Dagger 2 实例讲解(一)

本文原创,转载请注明出处:http://blog.csdn.net/zjbpku 关于Dagger,在之前的博文(Android 依赖注入:Dagger 实例讲解(Demo下载))中已有介绍, 本文说的Dagger 2主要是由Google技术 人员参与开发的,当然包括Square的各位及其他一些Contributors在内的大牛也贡献了不少.该项目大概是从去年11月份开始启动的,到目前该项 目还在继续进行,Snapshot version也是刚刚发布不久,从Github提供的内容看,不久会是Pr

Android进阶(二十三)Android开发过程之实例讲解

Android开发过程之实例讲解 前言 回过头来审视之前做过的Android项目,发觉自己重新开发时忽然间不知所措了,间隔了太长时间没有开发导致自己的Android技能知识急剧下降.温故而知新. 废话少说,进入正题~ 下面主要以自己之前开发过的Android小项目为例,探讨Android开发基本流程,以及其中所涉及到的原理. 项目名称为"我查查",主要的实现功能是查询.添加商品评价,分享购物体验. 主要界面如下: 图1 主功能界面 图2 查看商品信息 图3 添加新评论 图4 扫码操作

Android实例-手机安全卫士(十七)-自定义按钮背景样式

一.目标. 按钮(button)默认.按下.获取焦点等状态下,其背景均显示自定的图片.            二.代码实现. 1.在res文件夹下新建drawable文件夹,在新建的drawable文件夹下新建一个文件(右键-new-file),取名button.xml. 2.在新建的文件(button.xml)中 ①.指定xml版本为1.0,编码格式为utf-8(即第一行为:<?xml version="1.0" encoding="utf-8"?>)

ASP.NET MVC3 实例(六) 增加、修改和删除操作(二)

http://www.jquery001.com/asp.net-mvc3-instance-add-update-delete2.html 上篇我们在 ASP.NET MVC3 中实现了添加操作,由于时间关系没有完成修改.删除操作,我们新建了一个名为"Contact"的 Controller,并实现了添加方法,下边就让我们在此基础上来完成 ASP.NET MVC3 中的修改和删除操作. 首先,我们在 Contact 控制器类中添加一个名为 View()的方法,用来从 Contact

JEECG 简单实例讲解权限控制

JEECG简单实例讲解权限控制 博文地址:http://blog.itpub.net/30066956/viewspace-1868754/ 作者: 许国杰 一.业务背景 某公司要实现一个日志系统,用来了解员工的工作量饱和情况. 二.需求 1.角色分为:员工.经理两种. 2.员工每天在日志系统中填报工作总结,然后经理进行点评. 3.表单内容包含:姓名.日期.工作总结.个人笔记.日志点评. 三.业务权限描述 1. 员工可以进行日志填报.查看操作.(按钮控件权限) 2. 经理可以进行日志点评.查看操