Android编程之selector下设置背景属性值的方法

本文实例讲述了Android编程之selector下设置背景属性值的方法。分享给大家供大家参考,具体如下:

在res/drawable文件夹新增一个文件,此文件设置了图片的触发状态,你可以设置 state_pressed,state_checked,state_pressed,state_selected,state_focused,state_enabled 等几个状态:

android:state_pressed

Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。

android:state_focused

Boolean. "true" if this item should be used when the object is focused (such as when a button is highlighted using the trackball/d-pad); "false" if this item should be used in the default, non-focused state.
true,获得焦点时显示;false,没获得焦点显示默认。

android:state_selected

Boolean. "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected.
true,当被选择时显示该图片;false,当未被选择时显示该图片。

android:state_checkable

Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。

android:state_checked

Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。

android:state_enabled

Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。

android:state_window_focused

Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).
true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片。

<?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/button_pressed"/><!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused"/><!-- focused --> <itemandroid:drawable="@drawable/button_normal"/><!-- default --> </selector>

更多关于Android开发相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》

希望本文所述对大家Android程序设计有所帮助。

时间: 2024-08-14 08:12:55

Android编程之selector下设置背景属性值的方法的相关文章

Java通过反射机制动态设置对象属性值的方法_java

/** * MethodName: getReflection<br> * Description:解析respXML 在通过反射设置对象属性值 * User: liqijing * Date:2015-7-19下午12:42:55 * @param clzzName * @param respXML * @return * @throws ClassNotFoundException * @throws DocumentException * @throws IllegalArgumentE

Android中给按钮同时设置背景和圆角示例代码_Android

前言 最近在做按钮的时候遇到在给按钮设置一张图片作为背景的同时还要自己定义圆角,最简单的做法就是直接切张圆角图作为按钮就可以了,但是如果不这样该怎么办呢,看代码: 下面来看效果图 一.先建一个圆角的shape文件: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">

Android中给按钮同时设置背景和圆角示例代码

前言 最近在做按钮的时候遇到在给按钮设置一张图片作为背景的同时还要自己定义圆角,最简单的做法就是直接切张圆角图作为按钮就可以了,但是如果不这样该怎么办呢,看代码: 下面来看效果图 一.先建一个圆角的shape文件: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">

Android编程之ICS式下拉菜单PopupWindow实现方法详解(附源码下载)_Android

本文实例讲述了Android编程之ICS式下拉菜单PopupWindow实现方法.分享给大家供大家参考,具体如下: 运行效果截图如下: 右边这个就是下拉菜单啦,看见有的地方叫他 ICS式下拉菜单,哎哟,不错哦! 下面先讲一下实现原理: 这种菜单实际上就是一个弹出式的菜单,于是我们想到android PopupWindow 类,给他设置一个view 在弹出来不就OK了吗. PopupWindow 的用法也很简单 主要方法: 步骤1.new 一个实例出来,我们使用这个构造方法即可, 复制代码 代码如

Android编程之ICS式下拉菜单PopupWindow实现方法详解(附源码下载)

本文实例讲述了Android编程之ICS式下拉菜单PopupWindow实现方法.分享给大家供大家参考,具体如下: 运行效果截图如下: 右边这个就是下拉菜单啦,看见有的地方叫他 ICS式下拉菜单,哎哟,不错哦! 下面先讲一下实现原理: 这种菜单实际上就是一个弹出式的菜单,于是我们想到android PopupWindow 类,给他设置一个view 在弹出来不就OK了吗. PopupWindow 的用法也很简单 主要方法: 步骤1.new 一个实例出来,我们使用这个构造方法即可, 复制代码 代码如

Android编程之Application设置全局变量及传值用法实例分析_Android

本文实例讲述了Android编程之Application设置全局变量及传值用法.分享给大家供大家参考,具体如下: /** * 重写Application,主要重写里面的onCreate方法,就是创建的时候, * 我们让它初始化一些值,前段时间在javaeye里面看到过一个例子,与此相似, * 我做了些改进.听说外国开发者习惯用此初始化一些全局变量,好像在Activity * 一些类里面初始化全局变量的化,会遇到一些空指针的异常,当然,我没有遇到过. * 如果用此方法初始化的话,那么就可以避免那些

Android编程之ProgressBar圆形进度条颜色设置方法

本文实例讲述了Android ProgressBar圆形进度条颜色设置方法.分享给大家供大家参考,具体如下: 你是不是还在为设置进度条的颜色而烦恼呢--别着急,且看如下如何解决. ProgressBar分圆形进度条和水平进度条 我这里就分享下如何设置圆形进度条的颜色吧,希望对大家会有帮助. 源码如下: 布局文件代码: <ProgressBar android:id="@+id/progressbar" android:layout_width="wrap_content

Android编程之TabWidget选项卡用法实例分析_Android

本文实例讲述了Android编程之TabWidget选项卡用法.分享给大家供大家参考,具体如下: 1 概览 TabWidget与TabHost.tab组件一般包括TabHost和TabWidget.FrameLayout,且TabWidget.FrameLayout属于TabHost. 是否继承TabActivity的问题 实现步骤.两种实现方式,一种是将每个Tab的布局嵌在TabHost中的FrameLayout中,每个Tab的内容布局与显示都在FrameLayout中进行,缺点是布局会显得很

Android编程之TabWidget选项卡用法实例分析

本文实例讲述了Android编程之TabWidget选项卡用法.分享给大家供大家参考,具体如下: 1 概览 TabWidget与TabHost.tab组件一般包括TabHost和TabWidget.FrameLayout,且TabWidget.FrameLayout属于TabHost. 是否继承TabActivity的问题 实现步骤.两种实现方式,一种是将每个Tab的布局嵌在TabHost中的FrameLayout中,每个Tab的内容布局与显示都在FrameLayout中进行,缺点是布局会显得很