如何解决android以编程方式添加ImageView选择器
| 我想以编程方式而不是通过xml文件使用选择器状态更改ImageView背景(在我的情况下为渐变颜色)。 我想按默认状态到我的ImageView 为了创建渐变背景,我使用以下代码: GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.BOTTOM_TOP,new int[] {Color.parseColor(startColour),Color.parseColor(endColour)});
我该如何实现选择器?
谢谢
解决方法
这是从我的应用程序:
public static Drawable getButtonDrawableByScreenCathegory(Context con,ScreenCategory screenCategory,ButtonType buttonType) {
int normalStateResID = (int) GXConstants.NOT_ID;
int pressedStateResID = R.drawable.button_header_pressed;
switch (screenCategory) {
...
}
Drawable state_normal = con.getResources()
.getDrawable(normalStateResID).mutate();
Drawable state_pressed = con.getResources()
.getDrawable(pressedStateResID).mutate();
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[] { android.R.attr.state_pressed },state_pressed);
drawable.addState(new int[] { android.R.attr.state_enabled },state_normal);
return drawable;
}
要设置背景,例如,我打电话给按钮:
button.setBackgroundDrawable(GXUtils.getButtonDrawableByScreenCathegory(
this,mScreenCategory,ButtonType.MENU)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。