从Java code修改XML选择值Java、code、XML

由网友(依赖、沵给的温暖ら)分享简介:我想创建一个通用的图像选择了多个按钮。是否有一个选项来改变XML的?:从Java code机器人绘制资源I am trying to create a universal image selector for multiple buttons.Is there an option to change the xm...

我想创建一个通用的图像选择了多个按钮。 是否有一个选项来改变XML的?:从Java code机器人绘制资源

I am trying to create a universal image selector for multiple buttons. Is there an option to change the xml's "android:drawable" resource from java code?

推荐答案

使用StateListDrawable由code seeting选择为:

use StateListDrawable for seeting selector by code as:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
    getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
    getResources().getDrawable(R.drawable.normal));

imageView.setImageDrawable(states);  //YOUR IMAGE HERE
//AND FOR BUTTON
 button.setBackgroundDrawable(states);//FOR BUTTON
阅读全文

相关推荐

最新文章