我正在制作一个非常简单的小部件,它使用 ImageView 显示图像。
我的布局看起来像这样
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dip">
<!-- ImageView logo -->
<ImageView
android:id="@+id/logo"
android:src="@drawable/giants"
android:contentDescription="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitXY" />
</RelativeLayout>
我的 Widget Provider 类看起来像这样
public class LogoWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
// What Happens While App Goes Through Each Widget Id//
for (int currentWidgetId: appWidgetIds) {
// Define And Instantiate Variable RemoteViews widgetLogo//
RemoteViews widgetLogo = new RemoteViews(context.getPackageName(), R.layout.widget_logo);
// Update Widget//
appWidgetManager.updateAppWidget(currentWidgetId, widgetLogo);
}
}
}
我的 list 中的接收器看起来像这样
<!-- Widget Provider Logo -->
<receiver android:name=".LogoWidgetProvider">
<!-- Tell The App That There Is A Widget To Display -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<!-- Tell The App Where To Find Widget Data -->
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info_logo">
</meta-data>
</receiver>
最后,小部件提供程序 XML 如下所示
<!-- Basic Information For Widget Logo -->
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_logo"
android:minHeight="110dip"
android:minWidth="110dip"
android:minResizeWidth="40dip"
android:resizeMode="vertical|horizontal"
android:updatePeriodMillis="3600000"
android:widgetCategory="home_screen">
</appwidget-provider>
由于某种原因,小部件每次都会崩溃。如果我将 ImageView 切换到 Button,它工作正常。让小部件显示图像我缺少什么?
谢谢
最佳答案
我想通了。无论分辨率如何,您都可以使用任何图像,但需要将其放在 400 x 400 像素的框中。我用油漆垫来做。一旦它在框中,图像就会显示
关于java - 添加 ImageView 会导致 Android 应用小部件崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50270052/