Cha*_*ake 3 android statelistdrawable
我正在尝试以编程方式将a设置StateListDrawable为库项目的自定义视图的背景.这是我正在做的事情:
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);
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用:它总是绘制启用状态.我希望它来绘制按下时,我的状态按下按钮.我究竟做错了什么?
Hen*_*rik 20
我猜按钮在按下时仍然启用?
您可以尝试撤消订单:
sld.addState(new int[] { android.R.attr.state_pressed }, drawable);
sld.addState(new int[] { android.R.attr.state_enabled },
new ColorDrawable(onClickColor));
Run Code Online (Sandbox Code Playgroud)
可能正在绘制第一个目前有效的州.
如果您在按下时需要不同的背景,而在所有其他情况下需要另一个背景,您还可以使用:
sld.addState(new int[] { android.R.attr.state_pressed }, drawable);
sld.addState(new int[] { StateSet.WILD_CARD },
new ColorDrawable(onClickColor));
Run Code Online (Sandbox Code Playgroud)
另外:我刚刚对此进行了测试,以下测试代码对我有用:
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8247 次 |
| 最近记录: |