我正在尝试在 android 中创建一个小部件,当我点击图像时,图像会更改为一个新图像——一种切换图像。
但是当我点击它时,图像会覆盖旧图像而不是替换新图像。我不知道这是小部件的工作方式还是我做错了什么。
我的图像是半透明的,因此万一一个覆盖另一个可以看到重叠的图像。
这是我通过覆盖它在 OnReceive 中编写的代码:
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(iAlertConstant.ACTION_WIDGET_UPDATE_FROM_WIDGET)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.mywidget);
remoteViews.setImageViewResource(R.id.btnOnOff,R.drawable.offbtn);
ComponentName thisWidget = new ComponentName(context, EmergencyWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, remoteViews);
}
else
super.onReceive(context, intent);
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/widget_background"
android:paddingLeft="10.0dip"
android:paddingTop="8.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="8.0dip"
android:layout_width="fill_parent"
android:layout_height="72dp"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="10.0dip"
android:layout_marginRight="10.0dip"
android:id="@+id/emergencyWidget"
android:orientation="horizontal">
<ImageView
android:background="@drawable/offbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_gravity="right"
android:id="@+id/btnOnOff"
/>
</LinearLayout>
最佳答案
正如我所怀疑的,您在 ImageView
上使用了 background
属性。 background
和setImageViewResource
是不同的。相反,坚持使用相同的代码,但将布局更改为使用 src
而不是 background
。
关于android - 小部件中的图像重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742991/