StateListDrawable可以根据View的不同状态,更换不同的背景

可以应用如EditText,Button等中,以Button为例

系统中默认的按钮被按下的颜色和未点击时的颜色不一样,该种实现可以用Java代码和XML实现

以Java代码:

//……前面对Button的声明略去
okBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected));
cancelBtn.setBackgroundDrawable(addStateDrawable(this, R.drawable.btn_normal, R.drawable.btn_selected, R.drawable.btn_selected)); //……对应主要的代码
//当对应的View处于不同的状态时,对应的bacdground跟着变化
private StateListDrawable addStateDrawable(Context context, int idNormal, int idPressed, int idFocused) {
StateListDrawable sd = new StateListDrawable();
Drawable normal = idNormal == - ? null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == - ? null : context.getResources().getDrawable(idPressed);
Drawable focus = idFocused == - ? null : context.getResources().getDrawable(idFocused);
//注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
//所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了
sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
sd.addState(new int[]{android.R.attr.state_focused}, focus);
sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
sd.addState(new int[]{android.R.attr.state_enabled}, normal);
sd.addState(new int[]{}, normal);
return sd;
}

xml方式实现时,可以先了解下对应xml的语法

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android"

android:constantSize=["true" | "false"]

android:dither=["true" | "false"]

android:variablePadding=["true" | "false"] >

<item

android:drawable="@[package:]drawable/drawable_resource"

android:state_pressed=["true" | "false"]

android:state_focused=["true" | "false"]

android:state_selected=["true" | "false"]

android:state_active=["true" | "false"]

android:state_checkable=["true" | "false"]

android:state_checked=["true" | "false"]

android:state_enabled=["true" | "false"]

android:state_window_focused=["true" | "false"] />

</selector>

下面对应的具体实例,由于是做背景用,该xml将放于/res/drawable下面(StateList中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后)

<?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/btn_selected"/>
<item android:state_focused="true" android:drawable="@drawable/btn_selected"/>
<item android:state_enabled="true" android:drawable="@drawable/btn_normal"/>
<item android:drawable="@drawable/btn_normal" />
</selector>

在Button的xml中进行加载:

 <Button
android:id="@+id/canel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_cancel"
android:layout_margin="10dip"
android:layout_weight=""
android:textColor="#ffffffff"
android:textSize="15sp"
android:background="@drawable/button_drawable"
/>

或在java代码中加载:

okBtn.setBackgroundDrawable(R.drawable.button_drawable);  

动态更换view类的背景----StateListDrawable的应用的更多相关文章

  1. 动态更换view类的背景---StateListDrawable的应用

    StateListDrawable可以根据View的不同状态,更换不同的背景 可以应用如EditText,Button等中,以Button为例 系统中默认的按钮被按下的颜色和未点击时的颜色不一样,该种 ...

  2. 动态更换应用Icon

    转:原理1--activity-alias 在AndroidMainifest中,有两个属性: // 决定应用程序最先启动的Activity android.intent.action.MAIN // ...

  3. Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解

    废话不多说,本文将会层层深入给大家讲解如何动态的生成一个完整的界面. 本文内容: Java代码中动态生成View Java代码中动态设置View的位置,以及其他的属性 LayoutParams详解 一 ...

  4. 《Qt编程的艺术》——8.2.1 在Designer中使用View类

    不幸的是,QDirModel有一个严重的限制:因为view不响应鼠标操作,我们不得不自己建立这些功能.除此之外,每个view中,用户都一次只能选择一个元素.如果你想要允许同时选择多个项目,你也必须自己 ...

  5. 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)

    发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...

  6. view类的XML属性

    属性名称 对应方法 描述 android:background setBackgroundResource(int) 设置背景 android:clickable setClickable(boole ...

  7. Android游戏开发之旅 View类详解

    Android游戏开发之旅 View类详解 自定义 View的常用方法: onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) ...

  8. WPF通过DynamicResource实现给界面动态更换皮肤

    在我们的程序中有时候需要去实现动态更换皮肤的效果,从而完成一些个性化的设置,那么我们究竟怎样去实现动态换皮肤的效果呢?那么我们经常用到的就是设置不同的Style,并且在主程序的xaml文件中通过Dyn ...

  9. 【转】【iOS】动态更换App图标

    原文网址:http://www.cocoachina.com/ios/20170619/19557.html 前言 动态更换App图标这件事,在用户里总是存在需求的:有些用户喜欢“美化”自己的手机.至 ...

随机推荐

  1. Python核心编程读笔 11:模块

    第12章 模块 1.基本概念 模块的文件名就是模块名字.py 每个模块都定义了自己唯一的名称空间 模块的搜索路径:会被保存在 sys 模块的 sys.path 变量里 >>>sys. ...

  2. EC读书笔记系列之12:条款22、23、24

    条款22 将成员变量声明为private 记住: ★切记将成员变量声明为private.这可赋予客户访问数据的一致性.可细微划分访问控制.允诺约束条件获得保证,并提供class作者以充分的实现弹性. ...

  3. Rectangles

    Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have t ...

  4. QF——iOS程序运行原理(APP的生命周期)

    iOS程序的运行原理: main.m: 1. main.m 主函数是所有程序的入口函数. 2. 在main函数里是UIApplicationMain函数,开启了一个无限循环,以监听该应用. 该UIAp ...

  5. Git 系列(一):什么是 Git

    欢迎阅读本系列关于如何使用 Git 版本控制系统的教程!通过本文的介绍,你将会了解到 Git 的用途及谁该使用 Git. 如果你刚步入开源的世界,你很有可能会遇到一些在 Git 上托管代码或者发布使用 ...

  6. SQL Server中使用md5的方式

    在SQl2005下自带的函数hashbytes() ,此函数是微软在SQL SERVER 2005中提供的,可以用来计算一个字符串的 MD5 和 SHA1 值,使用方法如下: --获取123456的M ...

  7. Android 三星手机不能调起应用市场

    Uri uri; if (hasAnyMarketInstalled(getContext())) { uri = Uri.parse("market://details?id=" ...

  8. Android Studio 添加Assets目录

    Android Studio 添加Assets目录: 法一: Since Android Studio uses the new Gradle-based build system, you shou ...

  9. css3 transform 变形

    在css3中,用transform可以实现文字或图像的旋转.缩放.倾斜和移动,并且该元素下的所有子元素,随着父元素一样转.缩放.倾斜和移动. transform属性 transform的属性包括:ro ...

  10. Lucky Sum

    Description Lucky Sum time limit per test: 2 seconds memory limit per test: 256 megabytes input: sta ...