Android 代码实现Selector

有一些特殊情况需要动态设置Selector,所以用代码实现

XML Selector 如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" />
    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact_sel" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" />
    <!-- Pressed -->
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/contact_sel" />
    <item android:state_pressed="true" android:drawable="@drawable/contact_sel" />
</selector>

代码实现

        StateListDrawable drawable = new StateListDrawable();
        //Non focused states
        drawable.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                getResources().getDrawable(R.drawable.contact));
        drawable.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                getResources().getDrawable(R.drawable.contact_sel));
        //Focused states
        drawable.addState(new int[]{android.R.attr.state_focused,-android.R.attr.state_selected, -android.R.attr.state_pressed},
                getResources().getDrawable(R.drawable.contact_sel));
        drawable.addState(new int[]{android.R.attr.state_focused,android.R.attr.state_selected, -android.R.attr.state_pressed},
                getResources().getDrawable(R.drawable.contact_sel));
        //Pressed
        drawable.addState(new int[]{android.R.attr.state_selected, android.R.attr.state_pressed},
                getResources().getDrawable(R.drawable.contact_sel));
        drawable.addState(new int[]{android.R.attr.state_pressed},
                getResources().getDrawable(R.drawable.contact_sel));
         
        TextView textView = (TextView) findViewById(R.id.TextView_title);
                
        textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);

注意里面的“-”号,当XML的设定是false时,就需要使用资源符号的负值来设定。

我在项目里面的做法是这样的:

public void setIconDrawableSelector() {
        StateListDrawable drawable = new StateListDrawable();
        Drawable drawableNoSel = Drawable.createFromPath(mLocalArrayList.get(0));
        Drawable drawableSel = Drawable.createFromPath(mLocalArrayList.get(1));
        StateListDrawable drawable1 = new StateListDrawable();
        Drawable drawableNoSel1 = Drawable.createFromPath(mLocalArrayList.get(2));
        Drawable drawableSel1 = Drawable.createFromPath(mLocalArrayList.get(3));
        StateListDrawable drawable2 = new StateListDrawable();
        Drawable drawableNoSel2 = Drawable.createFromPath(mLocalArrayList.get(4));
        Drawable drawableSel2 = Drawable.createFromPath(mLocalArrayList.get(5));
        StateListDrawable drawable3 = new StateListDrawable();
        Drawable drawableNoSel3 = Drawable.createFromPath(mLocalArrayList.get(6));
        Drawable drawableSel3 = Drawable.createFromPath(mLocalArrayList.get(7));

        //Non focused states
        drawable.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableNoSel);
        drawable.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel);
        //Focused states
        drawable.addState(new int[]{android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel);
        drawable.addState(new int[]{android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel);

        ((ImageView) findViewById(mainBottomIds[0])).setImageDrawable(drawable);
        findViewById(mainBottomIds[0]).setSelected(true);

        //Non focused states
        drawable1.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableNoSel1);
        drawable1.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel1);
        //Focused states
        drawable1.addState(new int[]{android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel1);
        drawable1.addState(new int[]{android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel1);

        ((ImageView) findViewById(mainBottomIds[1])).setImageDrawable(drawable1);
        findViewById(mainBottomIds[1]).setSelected(false);

        //Non focused states
        drawable2.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableNoSel2);
        drawable2.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel2);
        //Focused states
        drawable2.addState(new int[]{android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel2);
        drawable2.addState(new int[]{android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel2);

        ((ImageView) findViewById(mainBottomIds[2])).setImageDrawable(drawable2);
        findViewById(mainBottomIds[2]).setSelected(false);

        //Non focused states
        drawable3.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableNoSel3);
        drawable3.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel3);
        //Focused states
        drawable3.addState(new int[]{android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel3);
        drawable3.addState(new int[]{android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},
                drawableSel3);

        ((ImageView) findViewById(mainBottomIds[3])).setImageDrawable(drawable3);
        findViewById(mainBottomIds[3]).setSelected(false);

    }

我其实只用到两个状态就够了,这里是我的底部导航动态加载。

转载请注明出处:http://www.jianshu.com/u/c864df301e25

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • 妈妈不在家的一天 今天是一个特殊的日子,因为妈妈不在,而爸爸却在,因为爸爸不会管我,但妈妈却要管我,所以妈妈...
    沈小丁子阅读 183评论 0 0
  • 时间:2017-1-24(周二) 地点:湖北武汉 修习者小豆丁:小俊娟 1我怎么如此幸运,快过年了,武汉的人少了,...
    余俊娟阅读 172评论 0 0