博客
关于我
selector
阅读量:219 次
发布时间:2019-02-28

本文共 2454 字,大约阅读时间需要 8 分钟。

Android状态选择器详解

1. 状态选择器的作用

状态选择器(StateListDrawable)是Android系统中一个强大的工具,用于为UI组件根据不同的状态定义不同的视觉效果。通过状态选择器,可以为View在不同状态下设置不同的背景图像或颜色,从而提升用户体验。

2. 常见状态定义

Android定义了多种状态,可以让View根据状态切换不同的外观。以下是常见的状态定义:

  • android:drawable:为View设置默认的背景图像或颜色。
  • android:state_pressed:表示View被按下(如按钮)。
  • android:state_focused:表示View获得焦点(如文本框)。
  • android:state_window_focused:表示应用程序是否在前台。
  • android:state_selected:表示View被选中(如ListView中的项)。
  • android:state_checkable:表示View是否可以被选中。
  • android:state_checked:表示View已经被选中(如RadioButton)。
  • android:state_enabled:表示View是否能接收触摸或点击事件。

3. Focused与Selected的区别

  • Focused状态:通常由按键操作引起,一个窗口只能有一个View获得焦点。
  • Selected状态:由应用程序主动调用setSelected()控制,可以有多个View处于selected状态。

4. 常用组件的状态设置

4.1 Checkbox设置

4.2 ImageButton设置

4.3 Button设置

4.4 TextView设置

5. StateListDrawable类功能

5.1 类结构

public class StateListDrawable extends DrawableContainer {    // DrawableContainer是Drawable的子类,用于管理多个Drawable对象}public class DrawableContainer extends Drawable implements Callback {    // Callback接口用于定义Drawable的回调方法}

5.2 常用方法

  • addState方法:用于为特定的状态集合设置Drawable图片资源。

    stalistDrawable.addState(new int[]{pressed, window_focused}, getResources().getDrawable(R.drawable.guide_1));
  • drawableStateChanged方法:根据当前View的状态属性值更新Drawable对象。

  • getState方法:获取当前View的状态属性值并返回状态集合。

  • setState方法:根据提供的状态集合更新Drawable对象,并触发状态变化回调。

6. 状态切换的实现

状态切换主要通过以下步骤实现:

  • 获取当前状态集合:调用getDrawableState()方法获取当前View的状态属性值。
  • 更新Drawable对象:调用setState()方法,根据新的状态集合找到匹配的Drawable对象。
  • 刷新View:调用invalidateSelf()方法刷新View的外观。
  • 7. 动态创建StateListDrawable

    除了使用XML文件,开发者还可以通过程序matic方式创建StateListDrawable对象:

    StateListDrawable stalistDrawable = new StateListDrawable();stalistDrawable.addState(new int[]{pressed, window_focused}, getResources().getDrawable(R.drawable.guide_1));// 其他添加状态的操作...Drawable mBackground = stalistDrawable;mBackground.setCallback(this); // 设置回调this.setBackgroundDrawable(null); // 去掉默认背景

    8. TextView颜色设置

    8.1 通过方法设置

    tv.setTextColor(Color.parseColor("#FFFFFF")); // 通过argb值设置tv.setTextColor(Color.rgb(255, 255, 255)); // 通过颜色分量设置tv.setTextColor(getResources().getColor(R.color.my_color)); // 通过资源引用设置

    8.2 通过ColorStateList设置

    Resources resource = getBaseContext().getResources();ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);if (csl != null) {    tv.setTextColor(csl);}

    8.3 通过XML文件设置

    #FFFFFF

    通过上述方法,开发者可以根据需求灵活设置TextView的颜色。

    转载地址:http://yyss.baihongyu.com/

    你可能感兴趣的文章
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>