0
点赞
收藏
分享

微信扫一扫

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

举报

相关推荐

0 条评论