StateListDrawable不起作用 [英] StateListDrawable not working

查看:91
本文介绍了StateListDrawable不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式将StateListDrawable设置为我的图书馆项目自定义视图的背景.这是我在做什么:

I'm trying to programatically set a StateListDrawable as the background of my custom view for a library project. Here's what I'm doing:

final TypedArray a = getContext().obtainStyledAttributes(attrs,
            R.styleable.ActionBar);
    int firstColor = a.getColor(
            R.styleable.ActionBar_backgroundGradientFirstColor, 0xff000000);
    int secondColor = a
            .getColor(R.styleable.ActionBar_backgroundGradientSecondColor,
                    0xff000000);
    int textViewColor = a.getColor(R.styleable.ActionBar_titleColor,
            0xffffffff);
    int onClickColor = a.getColor(
            R.styleable.ActionBar_backgroundClickedColor, 0xff999999);
    a.recycle();

    StateListDrawable sld = new StateListDrawable();
    GradientDrawable drawable = new GradientDrawable(
            Orientation.TOP_BOTTOM, new int[] { firstColor, secondColor });
    sld.addState(new int[] { android.R.attr.state_enabled },
            new ColorDrawable(onClickColor));
    sld.addState(new int[] { android.R.attr.state_pressed }, drawable);

    action2.setBackgroundDrawable(sld);
    action3.setBackgroundDrawable(sld);
    actionBack.setBackgroundDrawable(sld);
    pb.setBackgroundDrawable(drawable);
    tv.setBackgroundDrawable(drawable);
    tv.setTextColor(textViewColor);

但是,这不起作用:它始终绘制 enabled 状态.当我按下按钮时,我希望它绘制 Pressed 状态.我在做什么错了?

However, this is not working: It always draws the enabled state. I want it to draw the Pressed state when I press the button. What am I doing wrong?

推荐答案

我猜按钮被按下后仍处于启用状态?

I guess the button is still enabled while it's pressed?

您可以尝试反转顺序:

sld.addState(new int[] { android.R.attr.state_pressed }, drawable);
sld.addState(new int[] { android.R.attr.state_enabled },
        new ColorDrawable(onClickColor));

可能正在绘制第一个当前有效状态.

Probably the first currently valid state is being drawn.

如果要在按下背景时使用不同的背景,而在其他所有情况下也要使用其他背景,则可以使用:

If you want a different background when it's being pressed and another background for all other cases you can also use:

sld.addState(new int[] { android.R.attr.state_pressed }, drawable);
sld.addState(new int[] { StateSet.WILD_CARD },
        new ColorDrawable(onClickColor));

添加:我刚刚对此进行了测试,下面的测试代码对我有用:

Addition: I just tested this and the following test code works for me:

Button testButton = new Button(context);
            testButton.setText("Test");
            StateListDrawable sld = new StateListDrawable();
            GradientDrawable drawable = new GradientDrawable(
                    Orientation.TOP_BOTTOM, new int[] { Color.BLUE, Color.RED });
            sld.addState(new int[] { android.R.attr.state_pressed }, drawable);
            sld.addState(StateSet.WILD_CARD, new ColorDrawable(Color.YELLOW));
            testButton.setBackgroundDrawable(sld);          
            mainLayout.addView(testButton);

这篇关于StateListDrawable不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆