StateListDrawable、ColorStateList

知识点一:StateListDrawable类介绍

类功能说明:该类定义了不同状态值下与之对应的图片资源,即我们可以利用该类保存多种状态值,多种图片资源。

常用方法为:

public void addState (int[] stateSet, Drawable drawable)

功能: 给特定的状态集合设置drawable图片资源

使用方式:参考前面的hello_selection.xml文件,我们利用代码去构建一个相同的StateListDrawable类对象。

//初始化一个空对象
StateListDrawable stalistDrawable = new StateListDrawable();
//获取对应的属性值 Android框架自带的属性 attr
int pressed = android.R.attr.state_pressed;
int window_focused = android.R.attr.state_window_focused;
int focused = android.R.attr.state_focused;
int selected = android.R.attr.state_selected;
stalistDrawable.addState(newint []{pressed , window_focused}, getResources().getDrawable(R.drawable.pic1));
stalistDrawable.addState(newint []{pressed , -focused}, getResources().getDrawable(R.drawable.pic2);
stalistDrawable.addState(newint []{selected }, getResources().getDrawable(R.drawable.pic3);
stalistDrawable.addState(newint []{focused }, getResources().getDrawable(R.drawable.pic4);
//没有任何状态时显示的图片,我们给它设置我空集合
stalistDrawable.addState(newint []{}, getResources().getDrawable(R.drawable.pic5);

上面的“-”负号表示对应的属性值为false
当我们为某个View使用其作为背景色时,会根据状态进行背景图的转换。

public boolean isStateful ()

功能: 表明该状态改变了,对应的drawable图片是否会改变。
注:在StateListDrawable类中,该方法返回为true,显然状态改变后,我们的图片会跟着改变。
在Android应用的开发过程中,我们经常要根据控件的状态,改变控件的显示,比如EditeText在focus,或者是unfocus时设置背景颜色的不同,这个相信大家在以前肯定看到过,通过Selector就可以实现,但是如何让EditeText在focus,或者是unfocus时设置不同的颜色呢?接下来说的ColorStateList就是这样的功能。

新建一个项目,为了便于观察,在默认的main.xml文件里面拖入两个EditeText,然后在drawable文件夹中新建一个color_stat_list.xml,在里面输入以下内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="#FF0000" android:state_focused="true" ></item>
    <item android:color="#00FF00" android:state_focused="false" ></item>    
</selector>

编辑EditText,设置它们的android:textColor="@drawable/color_state_list",形如:

<EditText
       android:id="@+id/editText1"
       android:textColor="@drawable/color_state_list"
       android:text="@string/app_name"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" >

   </EditText>

      <EditText
       android:id="@+id/editText1"
       android:textColor="@drawable/color_state_list"
       android:text="@string/app_name"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" >

   </EditText>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您编写一个工具,提供修改dialog的默认背景颜色的方法。具体实现如下: ```java public class DialogUtils { /** * 修改dialog的默认背景颜色 * * @param dialog dialog对象 * @param color 颜色值 */ public static void setDialogBackground(Dialog dialog, int color) { Window window = dialog.getWindow(); if (window != null) { // 获取窗口背景drawable Drawable background = window.getDecorView().getBackground(); // 判断背景drawable型 if (background instanceof ColorDrawable) { // 如果是颜色drawable,则直接设置颜色值 ((ColorDrawable) background).setColor(color); } else { // 否则,将背景drawable转换为StateListDrawable,并设置颜色值 StateListDrawable stateListDrawable = (StateListDrawable) background; int[] colors = new int[]{color}; int[][] states = new int[][]{{android.R.attr.state_enabled}}; ColorStateList colorStateList = new ColorStateList(states, colors); stateListDrawable.setColor(colorStateList); } } } } ``` 使用方法: ```java Dialog dialog = new Dialog(context); // 设置dialog布局等属性... // 设置默认背景颜色为绿色 DialogUtils.setDialogBackground(dialog, Color.GREEN); dialog.show(); ``` 以上代码中,`setDialogBackground`方法实现了修改dialog默认背景颜色的功能。在该方法中,我们通过`getWindow`获取dialog的窗口对象,然后获取窗口背景drawable。接着,根据背景drawable型,分别进行处理:如果是颜色drawable,则直接设置颜色值;否则,将背景drawable转换为StateListDrawable,并设置颜色值。最后,在使用dialog时,只需要调用`setDialogBackground`方法即可完成默认背景颜色的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值