如何使 AppCompatImageView 在小部件中工作?

How to make AppCompatImageView works in widget?

我尝试使用 AppCompatImageView 在小部件中显示矢量图像,但未显示图像(相反,我收到了一条剪切消息 "Error loading...")。同一段代码在我的主 activity 中运行良好(两者都在 LinearLayout 中)。小部件有限制吗?我想念什么?

我的minSdkVersion是19,我用的是androidx

我尝试将布局更改为标准的 LinearLayout。

我的XML:

<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/widget_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/appwidget_image"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:contentDescription="@+id/appwidget_label"
        android:padding="5dp"
        app:srcCompat="@drawable/ic_baseline_toggle_off_24px"
        android:tint="@color/colorAccent"/>

    <TextView
        android:id="@+id/appwidget_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="center_horizontal"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:singleLine="true"
        android:text="@string/appwidget_default_text"
        android:textAlignment="gravity"
        android:textColor="@color/colorAccent"
        android:textSize="12sp" />

</androidx.appcompat.widget.LinearLayoutCompat>

我的摘录 build.gradle :

android {
    compileSdkVersion 28
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'
    implementation 'androidx.appcompat:appcompat-resources:1.1.0-alpha05'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'de.greenrobot:eventbus:2.4.0'
    implementation 'com.j256.ormlite:ormlite-android:4.48'
    implementation 'androidx.work:work-runtime:2.0.1'
}

logcat中的一些消息我不明白(但与我的问题无关):

05-21 19:38:09.886 12404-12404/com.yadoms.widgets.statedisplay E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering
05-21 19:38:09.886 12404-12404/com.yadoms.widgets.statedisplay W/dalvikvm: VFY: unable to resolve instanceof 216 (Landroid/graphics/drawable/RippleDrawable;) in Landroidx/appcompat/widget/AppCompatImageHelper;

05-21 19:38:09.906 12404-12404/com.yadoms.widgets.statedisplay E/dalvikvm: Could not find class 'android.view.textclassifier.TextClassificationManager', referenced from method androidx.appcompat.widget.AppCompatTextClassifierHelper.getTextClassifier

05-21 19:38:10.066 12404-12404/com.yadoms.widgets.statedisplay E/EGL_emulation: tid 12404: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)

感谢您的帮助

Is there a limitation for widgets ?

是的。您仅限于小部件名册 listed in the documentation.

特别是,您不能使用任何自定义小部件,无论是您自己的小部件还是来自库(例如 AndroidX)的小部件。那是因为您的应用没有呈现应用小部件 — 主屏幕是。