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用户界面的一 ...
随机推荐
- linux命令之chown命令
发布:JB01 来源:脚本学堂 [大 中 小] 本文介绍下,linux系统中用于文件与目录权限管理的命令 chown命令的用法,chown将指定文件的拥有者改为指定的用户或组.有需要的朋友 ...
- Objective-C中的const ,extern,static
一.const 1>对于const,记住关键的一点,它只是修饰右边的变量. 例如: - (void)viewDidLoad { [super viewDidLoad]; // const两种用法 ...
- App在后台运行
App有三种状态: 1. 死亡状态(未打开App); 2. 前台运行状态(打开状态); 3. 后台暂停状态(停止所有动画, 定时器, 多媒体联网等操作) 4. 后台运行状态(后台运行); ------ ...
- lua之mysql编程
环境搭建 #mysql 安装 sudo apt-get install mysql-server mysql-client # lua mysql 开发驱动安装 sudo apt-get instal ...
- #include<filename.h> 与 #include“filename.h”
#include<filename.h>:从标准库路径去寻找该文件,对于VC来说,应该还包括VC环境设置选项中的包含目录以及工程属性中指定的目录. #include“filename.h” ...
- android中viewPager+fragment实现的屏幕左右切换(进阶篇)
Fragment支持在不同的Activity中使用并且可以处理自己的输入事件以及生命周期方法等.可以看做是一个子Activity. 先看一下布局: 1 <LinearLayout xmlns:a ...
- tornado异步请求非阻塞
前言也许有同学很迷惑:tornado不是标榜异步非阻塞解决10K问题的嘛?但是我却发现不是torando不好,而是你用错了 比如最近发现一个事情:某网 前言 也许有同学很迷惑:tornado不是标榜异 ...
- 【HDOJ】2473 Junk-Mail Filter
并查集删除结点,方法是构建虚拟点,做映射. #include <stdio.h> #include <string.h> #define MAXNUM 1000050 int ...
- 正确使用TeamViewer VPN
http://www.mcbbs.net/thread-222996-1-1.html
- 【Android 复习】 : Activity之间传递数据的几种方式
在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...