我正在嘗試在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的背景設置爲最初的圖像,而不是使用[src](http://developer.android.com/reference/android/widget/ImageView.html#attr_android: src)標籤? – kabuko 2012-03-16 19:25:25
@kabuko抱歉沒有讓你 – Hunt 2012-03-16 19:26:49
你可以發佈你的小部件的佈局? – kabuko 2012-03-16 19:27:38