android - 小部件中的图像重叠

标签 android android-widget

我正在尝试在 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 属性。 backgroundsetImageViewResource 是不同的。相反,坚持使用相同的代码,但将布局更改为使用 src 而不是 background

关于android - 小部件中的图像重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742991/

相关文章:

java - 使用 `==` 而不是 `equals()` 比较字符串时,如何使 Android Studio 或 IntelliJ 显示错误?

android - 连字符将一行分成多行

android - 自定义微调器项目未作为微调器的一部分出现

Android:带有链接链接的可点击 TextView

android - android教程中的FFMpeg命令

android - 从相机拍摄的图像中获取图像

Android:暂停录音机并恢复

android - 如何将相对布局停靠到屏幕底部

Android - VideoView 需要按 BACK 两次才能退出

android - OS X 10.11/El Capitan 中的英特尔 HAXM/Android Studio