Android应用开发学习之Selector
作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
本文我们来看一个通过selector动态改变ImageButton背景图片的例子,下图是该程序的运行效果:
该程序中有三个ImageButton,最上面的是鼠标按下时的状态;中间的是鼠标没有按下,但是按钮获得了焦点的状态;最下面的是鼠标没有按下,按钮也不具有焦点的状态。
先来看主布局文件main.xml,其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ImageButton
android:id="@+id/button1"
android:src="@drawable/button_image_selector"
android:focusableInTouchMode="true"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton> <ImageButton
android:id="@+id/button2"
android:src="@drawable/button_image_selector"
android:focusableInTouchMode="true"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton> <ImageButton
android:id="@+id/button3"
android:src="@drawable/button_image_selector"
android:focusableInTouchMode="true"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton> </LinearLayout>
在线性布局控件中,放置三个ImageButton,android:src属性指定了背景图片选择器button_image_selector.xml,android:focusableInTouchMode属性标明按钮可以获得焦点。android:background="#0000"属性是为了去掉按钮背后的灰色边框,但是这样做,如果不用背景选择器的话,会看不出点击效果。
按钮背景图片选择器button_image_selector.xml定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/image01"/>
<item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/image02"/>
<item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/image03"/>
</selector>
item1指定了鼠标按下时的背景图片,item2指定了鼠标没有按下,但是获得焦点时的背景图片,item3指定了鼠标没有按下,且没有焦点时的背景图片。
主Activity文件没有什么特别的,其内容如下:
package com.liuhaoyu; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Android应用开发学习之Selector的更多相关文章
- Android应用开发学习之表格视图
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来学习一个使用表格视图的程序,下图是该程序的运行效果: 该程序主Activity文件内容如下: packag ...
- 2021年正确的Android逆向开发学习之路
2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...
- Android应用开发学习笔记之播放音频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...
- android移动开发学习笔记(二)神奇的Web API
本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...
- Android应用开发学习笔记之事件处理
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android提供的事件处理机制分为两类:一是基于监听的事件处理:二是基于回调的事件处理.对于基于监听的事件处理,主 ...
- Android 系统开发学习杂记(转)
http://blog.csdn.net/shagoo/article/details/6709430 > 开发环境1.安装 Eclipse 和 android-sdk 并解压安装2.Eclip ...
- Android应用开发学习之相对布局
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边.右边.上边或下 ...
- Android应用开发学习笔记之AsyncTask
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...
- Android应用开发学习笔记之Fragment
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Fragment翻译成中文就是“碎片”.“片断”的意思,Fragment通常用来作为一个Activity用户界面的一 ...
随机推荐
- thinkphp多表查询
在学习thinkphp 的过程中,需要对多表进行操作,但是在实际过程中,总是遇到各种问题,所以写下这篇博文,作为自己的学习历程 在操作过程中,两表查询都没有问题,但是三表查询就开始出现问题 有以下三张 ...
- Config IIS server6.0-- HTTP 错误 500.21 - Internal Server Error 解决方案
HTTP 错误 500.21 - Internal Server Error 解决方案 不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发 ...
- python【第十九篇】Django进阶
1.路由系统优化 1.1 路由分发 前面我们已经知道,在工程名下的urls.py中写我们的路由映射关系,那么问题来了,假设我们有10个app,如果把所有的url映射都写在urls.py文件中,那么每一 ...
- MINA源码阅读之ACP
Processor在XXAcceptor以及XXConnector中所扮演的只能就是:作为Acceptor以及Connetor所创建的Session的Processor: IoAcceptor作为他所 ...
- asp.net MVC FileResult在IE下异常的解决办法
var encoding = System.Text.Encoding.UTF8; Response.Charset = encoding.WebName; Response.HeaderEncodi ...
- asp.net与MVC4的路由原理和过程【学习笔记】
当IIS收到一个http请求,把请求信息发给对应的HttpModel(实际是实现类UrlRoutingModule),在HttpModel中会注册HttpApplication 类中的PostReso ...
- Java编程规范整理
分享一份网友整理的编程过程中的命名规范 包命名 包名按照域名的范围从大到小逐步列出,恰好和Internet上的域名命名规则相反. 由一组以"."连接的标识符构成,通常第一个标识符为 ...
- 根据WSDL生成代理类方式(2)
运行开发人员工具提示 输入命令行svcutil http://localhost:8080/Test/TestClassPort?wsdl
- 一周一话题之三(Windows服务、批处理项目实战)
-->目录导航 一. Windows服务 1. windows service介绍 2. 使用步骤 3. 项目实例--数据上传下载服务 二. 批处理运用 1. 批处理介绍 2. 基本语法 3. ...
- [转贴]JAVA:RESTLET开发实例(三)基于spring的REST服务
前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将介绍restlet如何整合spring框架进行开发.Spring 是一个开源框架,是为了解决企业应 ...