【发布时间】:2025-12-07 00:40:01
【问题描述】:
我正在尝试制作一个仅包含一个 imageView 的小部件,当您点击它时,它会变为另一张图片,从我制作的六张图片中随机选择。
我的问题是,对于如何制作小部件没有很好的解释,所以我不知道该怎么做。
我已经做了一个小部件布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:clickable="true"
android:contentDescription="@string/widget_description"
android:onClick="widgetOnClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"
app:srcCompat="@mipmap/ic_widget" />
</LinearLayout>
我尝试在我的 .java 文件中编写一些代码,但没有任何作用。
package com.ggblbl.widget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;
import java.util.Random;
/**
* Implementation of App Widget functionality.
*/
public class RandomSideWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.random_side_widget);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
public void widgetOnClick(View v) {
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
Random rnd = new Random();
int number = rnd.nextInt(6) + 1;
switch(number) {
case 1:
imageView.setImageResource(R.drawable.widget_blue);
break;
case 2:
imageView.setImageResource(R.drawable.widget_green);
break;
case 3:
imageView.setImageResource(R.drawable.widget_orange);
break;
case 4:
imageView.setImageResource(R.drawable.widget_pink);
break;
case 5:
imageView.setImageResource(R.drawable.widget_red);
break;
case 6:
imageView.setImageResource(R.drawable.widget_yellow);
break;
default:
imageView.setImageResource(R.mipmap.ic_widget);
break;
}
}
}
我使用 API 级别 15。
【问题讨论】:
-
developer page on App Widgets 比较详细。你经历过吗?
-
我确实试过了(多次),是的,但我不太明白。
标签: android android-studio imageview android-widget