Android 安卓-我能';t更改我的ImageButton的ImageResource
因此,正如标题所说,即使我调用了setImageResource,我的ImageButton也不会更新其资源 这是我的密码:Android 安卓-我能';t更改我的ImageButton的ImageResource,android,imagebutton,android-image,Android,Imagebutton,Android Image,因此,正如标题所说,即使我调用了setImageResource,我的ImageButton也不会更新其资源 这是我的密码: public static void toggleqsring() { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.activ
public static void toggleqsring() {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_main, null, false);
SquareButton qs_ringer = (SquareButton) v.findViewById(R.id.qs_ring);
if (mPhoneIsSilent==0) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_off);
} else if (mPhoneIsSilent==1) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_on);
} else if (mPhoneIsSilent==2) {
qs_ringer.setImageResource(R.drawable.ic_qs_vibrate_on);
}
}
SquareButton是一个扩展ImageButton的类,但我已使其高度等于宽度
我不知道我做错了什么,有人能帮忙吗?试试这个:
qs_ringer.setBackgroundResource(R.drawable.ic_qs_ring_off);
好的,我做了一点实验后,它开始工作了 我没有传递MainActivity的上下文,而是将活动本身传递给一个新变量
public static Activity mActivity;
并使用此代码更改图像资源。由于findViewById是一种活动方法,但不是上下文,因此只能使用活动变量:
public static void toggleqsring() {
SquareButton qs_ringer = (SquareButton) mActivity.findViewById(R.id.qs_ring);
if (mPhoneIsSilent==0) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_off);
} else if (mPhoneIsSilent==1) {
qs_ringer.setImageResource(R.drawable.ic_qs_ring_on);
} else if (mPhoneIsSilent==2) {
qs_ringer.setImageResource(R.drawable.ic_qs_vibrate_on);
}
}
不,那只是将我的图片设置为背景,它不起作用。