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

时间:2019-05-21 19:45:42

标签: android widget android-imageview android-appcompat androidx

我尝试使用AppCompatImageView在小部件中显示矢量图像,但不显示图像(而是出现剪切消息“错误加载中……”)。同一段代码在我的主要活动中效果很好(都在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)

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

  

小部件有限制吗?

是的。您只能使用小部件listed in the documentation的花名册。

尤其是,您不能使用任何自定义窗口小部件,无论是您自己的还是来自库的(例如,AndroidX)。那是因为您的应用程序没有呈现应用程序小部件,而是主屏幕。

相关问题