<span style="font-size:24px;">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</span>
通过代码改变drawable 各种状态drawable
最新推荐文章于 2022-03-25 19:14:46 发布
本文介绍如何通过代码方式使用StateListDrawable为Android控件设置不同状态下的背景。示例中展示了为ImageView和Button设置pressed、focused及normal三种状态的具体实现。
325

被折叠的 条评论
为什么被折叠?



